1.0.2 • Published 1 year ago

canvas-syntax-highlight v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Canvas Syntax Highlight

Syntax highlighting on the canvas

Installation

npm

$ npm i canvas-syntax-highlight --save

or yarn

$ yarn add canvas-syntax-highlight

Usage

const fs = require('fs')
const { Canvas } = require('canvas')
const { drawHighlightedCode } = require('canvas-syntax-highlight')

const canvas = new Canvas(200, 200)
const ctx = canvas.getContext('2d')

ctx.fillStyle = '#fff'
ctx.fillRect(0, 0, 200, 200)

const code = {
    language: 'js',
    theme: 'light',
    code: `
// The Ultimate Question of Life, the Universe, and Everything
const TheUltimateQuestionOfLife = () => {
    return 42
}

console.log(TheUltimateQuestionOfLife())
    `.trim()
}

drawHighlightedCode(ctx, code, 10, 10)

canvas.createJPEGStream().pipe(fs.createWriteStream('./image.jpg'))

Result npm.io