mirror of
https://github.com/alibaba/GCanvas.git
synced 2025-12-08 17:36:42 +00:00
39 lines
1.2 KiB
JavaScript
39 lines
1.2 KiB
JavaScript
const { createCanvas, Image } = require('../export')
|
|
const canvas = createCanvas(400, 400);
|
|
const ctx = canvas.getContext('2d')
|
|
const fs = require('fs')
|
|
const path = require('path');
|
|
|
|
ctx.fillRect(0, 0, 150, 150) // Draw a rectangle with default settings
|
|
ctx.save() // Save the default state
|
|
|
|
ctx.fillStyle = '#09F' // Make changes to the settings
|
|
ctx.fillRect(15, 15, 120, 120) // Draw a rectangle with new settings
|
|
|
|
ctx.save() // Save the current state
|
|
ctx.fillStyle = '#FFF' // Make changes to the settings
|
|
ctx.globalAlpha = 0.5
|
|
ctx.fillRect(30, 30, 90, 90) // Draw a rectangle with new settings
|
|
|
|
ctx.restore() // Restore previous state
|
|
ctx.fillRect(45, 45, 60, 60) // Draw a rectangle with restored settings
|
|
|
|
ctx.restore() // Restore original state
|
|
ctx.fillRect(60, 60, 30, 30) // Draw a rectangle with restored settings
|
|
ctx.fillStyle="#ff0000"
|
|
ctx.fillRect(0,0,canvas.width,canvas.height);
|
|
|
|
fs.writeFile(`${path.join(__dirname, "..")}/tobuffer.png`, canvas.toBuffer("image/png"), err => {
|
|
if (err) {
|
|
throw err;
|
|
}
|
|
});
|
|
|
|
fs.writeFile(`${path.join(__dirname, "..")}/tobuffer.jpg`, canvas.toBuffer("image/jpeg"), err => {
|
|
if (err) {
|
|
throw err;
|
|
}
|
|
});
|
|
|
|
var buffer=canvas.toBuffer("raw")
|
|
console.log(buffer) |