Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
node-skia-canvas
Advanced tools
node-skia-canvas
is a W3C Canvas API implementation for Node.js. It is based on Google skia, and written with Node-API, provide similar canvas development just like on the browser.
There is already a Canvas works on node, but here is some enhancement, and take this package as a replacemnt of node-canvas
:
The project is written by C++ style API with node-addon-api
, and built with cmake-js
, and use prebuild
and prebuild-install
to prevent user enviroment building.
Consider about the complex host environment, so this package decide to let skia deps be static linked, compiling features could check the build script scripts/build_tools/build_skia.js
in this project.
npm install --save node-skia-canvas
const { createCanvas } = require('node-skia-canvas')
const canvas = createCanvas(256, 256)
const ctx = canvas.getContext('2d')
ctx.fillStyle = '#FFF'
ctx.fillRect(0, 0, 256, 256)
ctx.fillStyle = '#4285F4'
ctx.fillRect(10, 10, 100, 160)
ctx.fillStyle = '#0F9D58'
ctx.beginPath()
ctx.arc(180, 50, 25, 0, 2 * Math.PI)
ctx.closePath()
ctx.fill()
ctx.fillStyle = '#DB4437'
ctx.beginPath()
ctx.ellipse(100, 160, 50, 80, 0, 0, 2 * Math.PI)
ctx.closePath()
ctx.fill()
ctx.lineWidth = 5
ctx.strokeStyle = '#F4B400'
ctx.strokeRect(80, 50, 100, 160)
And get result:
You could run example by the script in scripts
folder:
node scripts/run_example.js skia_showcase
Implement the Standard Canvas API:
Canvas
CanvasRenderingContext2D
CanvasGradient
(not expose)CanvasPattern
(not expose)DOMMatrix
TextMetrix
(not expose)And also provide some useful module:
Image
, for image useregisterFont
, for font managementDiffer from the creation of canvas in DOM, we create canvas by instantiation Canvas
, or use createCanvas
:
// Instantication
const canvas = new Canvas(300, 300)
// or use factory method
const canvas = createCanvas(300, 300)
Properties:
Methods:
toBuffer(mimeType = 'image/png')
Not like canvas tag in browser, we need get real buffer information as result on server, so you could use this API, and it now support JPEG/PNG by pass mimeType, image/png
will be used if format is not specified.
const canvas = new Canvas(300, 300)
const ctx = canvas.getContext('2d')
// some canvas operation...
canvas.toBuffer('image/png') // or image/jpeg
Use it just like in browser, the API is almost the same (only support 2d
for now, gl
context is working in progress):
const canvas = new Canvas(300, 300)
const ctx = canvas.getContext('2d')
ctx.fillStyle = '#FFF'
ctx.fillRect(0, 0, 256, 256)
Properties:
Methods:
textDecoration
Enhanced feature for text rendering, provide text-decoration
property and use just like the property in CSS: <'text-decoration-line'> || <'text-decoration-style'> || <'text-decoration-color'> || <'text-decoration-thickness'>
const canvas = createCanvas(image.width, image.height)
const ctx = canvas.getContext('2d')
ctx.fillStyle = '#000'
ctx.textBaseline = 'top'
ctx.font = '24px/1.2 "PingFang SC"'
ctx.textDecoration = 'line-through'
fillText(x, y, text, [maxWidth])
Provide extra parameter maxWidth
to provide word-wrap typography, it is documented on MDN, but not implemented in most browser, but this library implemented this extra parameter.
Here is a helper class for image usage, and used in drawImage
as parameter:
const fs = require('fs')
const { Image } = require('node-skia-canvas')
const imgData = fs.readFileSync('./examples/leize.jpeg')
const image = new Image()
image.src = imgData
const canvas = createCanvas(image.width, image.height)
const ctx = canvas.getContext('2d')
ctx.drawImage(img, 0, 0, image.width, image.height)
Properties
There is an API loadImage
provided by node-canvas
, but you may have your own favorite network library like: urllib
、universal-fetch
, etc. So this loadImage
helper api may not that helpful, so it will not be provided currently.
We provide font management API and compat with node-canvas
:
registerFont(path, [{ family, weight }])
Register font to canvas, if family
or weight
is not provided, it will be parsed from font file automatically.
const { registerFont } = require('node-skia-canvas')
registerFont(path.join(__dirname, './OswaldRegular.ttf'), {
family: 'Oswald'
})
registerFont(path.join(__dirname, './OswaldLight.ttf'))
registerFont(path.join(__dirname, './OswaldBold.ttf'))
Thanks to node-canvas
for their test cases.
MIT
FAQs
Skia based graphics canvas run on node.js, written in Node-API
The npm package node-skia-canvas receives a total of 7 weekly downloads. As such, node-skia-canvas popularity was classified as not popular.
We found that node-skia-canvas demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.