Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-skia-canvas

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-skia-canvas

Skia based graphics canvas run on node.js, written in Node-API

  • 0.1.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7
increased by600%
Maintainers
1
Weekly downloads
 
Created
Source

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.

xx badge badge MIT

Introduction

There is already a Canvas works on node, but here is some enhancement, and take this package as a replacemnt of node-canvas:

  • Using Node-API to solve the ABI issue
  • Some skia module extension, for example: skparagraph, pathkit, sklottie
  • Working in node worker-thread to provide the possibility of multi-thread processing (Working in progress)
  • GPU context support (Working in progress)

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.

Install

npm install --save node-skia-canvas

Example

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:

5uiU2R.png

You could run example by the script in scripts folder:

node scripts/run_example.js skia_showcase

API Documentation

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 use
  • registerFont, for font management

Canvas

Differ 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

CanvasRenderingContext2D

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.

Image

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

About loadImage

There is an API loadImage provided by node-canvas, but you may have your own favorite network library like: urllibuniversal-fetch, etc. So this loadImage helper api may not that helpful, so it will not be provided currently.

Font Management

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'))

Acknowledgements

Thanks to node-canvas for their test cases.

License

MIT

FAQs

Package last updated on 08 Nov 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc