Last active
March 18, 2024 19:02
-
-
Save nilzona/e611c99336d8ea1f645bd391a459c24f to your computer and use it in GitHub Desktop.
node express app with Path2D polyfilled `node-canvas`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import express from 'express' | |
import { createCanvas, CanvasRenderingContext2D } from "canvas"; | |
import { applyPath2DToCanvasRenderingContext } from "path2d"; | |
applyPath2DToCanvasRenderingContext(CanvasRenderingContext2D); | |
// Path2D features has now been added to CanvasRenderingContext2D | |
const app = express() | |
const port = 3000 | |
const canvas = createCanvas(250, 200) | |
const ctx = canvas.getContext('2d') | |
// Write "Awesome!" | |
ctx.font = '30px Impact' | |
ctx.rotate(0.1) | |
ctx.fillText('Awesome CJS!', 50, 100) | |
const p = new Path2D("M10 10 l 20 0 l 0 20 Z"); | |
ctx.fillStyle = "green"; | |
ctx.fill(p); | |
ctx.beginPath(); | |
ctx.roundRect(50, 10, 40, 20, 5); | |
ctx.fillStyle = "blue"; | |
ctx.fill(); | |
// Draw line under text | |
var text = ctx.measureText('Awesome CJS!'); | |
ctx.strokeStyle = 'rgba(0,0,0,0.5)' | |
ctx.beginPath() | |
ctx.lineTo(50, 102) | |
ctx.lineTo(50 + text.width, 102) | |
ctx.stroke() | |
app.get('/', function(req, res) { | |
res.send(` | |
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>node canvas</title> | |
</head> | |
<body> | |
<img src="${canvas.toDataURL()}" /> | |
</body> | |
</html> | |
`) | |
}) | |
app.listen(port, function() { | |
console.log(`Example app listening on port ${port}`) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment