Skip to content

Commit

Permalink
feat: add node-canvas compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
DjDeveloperr committed Nov 30, 2020
1 parent e5df603 commit fbc8cb3
Show file tree
Hide file tree
Showing 5 changed files with 3,902 additions and 3,885 deletions.
36 changes: 17 additions & 19 deletions examples/image.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import Canvas, { CanvasRenderingContext2D, dataURLtoFile, loadImage } from '../mod.ts'
import { serve } from "https://deno.land/[email protected]/http/server.ts";

const canvas = Canvas.MakeCanvas(200, 200);
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D;

ctx.fillStyle = 'red';
ctx.fillRect(10, 10, 200 - 20, 200 - 20);

const img = await loadImage("https://cdn.discordapp.com/emojis/587737413330272291.gif?v=1");
ctx.drawImage(img, 100 - img.width() / 2, 100 - img.height() / 2);

const data = dataURLtoFile(canvas.toDataURL());

const server = serve({ hostname: "0.0.0.0", port: 8080 });
console.log(`HTTP webserver running. Access it at: http://localhost:8080/`);

for await (const request of server) {
request.respond({ status: 200, body: data });
import Canvas, { loadImage } from '../mod.ts'
import { serve } from "https://deno.land/[email protected]/http/server.ts";

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

ctx.fillStyle = 'red';
ctx.fillRect(10, 10, 200 - 20, 200 - 20);

const img = await loadImage("https://cdn.discordapp.com/emojis/587737413330272291.gif?v=1");
ctx.drawImage(img, 100 - img.width() / 2, 100 - img.height() / 2);

const server = serve({ hostname: "0.0.0.0", port: 8080 });
console.log(`HTTP webserver running. Access it at: http://localhost:8080/`);

for await (const request of server) {
request.respond({ status: 200, body: canvas.toBuffer() });
}
30 changes: 14 additions & 16 deletions examples/square.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import Canvas, { CanvasRenderingContext2D, dataURLtoFile } from 'https://deno.land/x/[email protected]/mod.ts'
import { serve } from "https://deno.land/[email protected]/http/server.ts";

const canvas = Canvas.MakeCanvas(200, 200);
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D;

ctx.fillStyle = 'red';
ctx.fillRect(10, 10, 200 - 20, 200 - 20);

const data = dataURLtoFile(canvas.toDataURL());

const server = serve({ hostname: "0.0.0.0", port: 8080 });
console.log(`HTTP webserver running. Access it at: http://localhost:8080/`);

for await (const request of server) {
request.respond({ status: 200, body: data });
import Canvas from '../mod.ts'
import { serve } from "https://deno.land/[email protected]/http/server.ts";

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

ctx.fillStyle = 'red';
ctx.fillRect(10, 10, 200 - 20, 200 - 20);

const server = serve({ hostname: "0.0.0.0", port: 8080 });
console.log(`HTTP webserver running. Access it at: http://localhost:8080/`);

for await (const request of server) {
request.respond({ status: 200, body: canvas.toBuffer() });
}
5 changes: 5 additions & 0 deletions lib.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 27 additions & 25 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import * as lib from "./lib.js";
import { CanvasKit } from "./types.ts";
import { fetchAuto } from './deps.ts'

const Canvas = await lib.CanvasKitInit({}) as CanvasKit;

export function dataURLtoFile(dataurl: string) {
let arr: string[] = dataurl.split(',');
let bstr = atob(arr[1]);
let n = bstr.length;
let data = new Uint8Array(n);
while(n--){
data[n] = bstr.charCodeAt(n);
}
return data;
}

export async function loadImage(url: string) {
const base64 = await fetchAuto(url);
const img = Canvas.MakeImageFromEncoded(dataURLtoFile(base64));
if(!img) throw new Error("Invalid Image");
return img;
}

export * from "./types.ts"
import * as lib from "./lib.js";
import { CanvasKit } from "./types.ts";
import { fetchAuto } from './deps.ts'

const Canvas = await lib.CanvasKitInit({}) as CanvasKit;

export function dataURLtoFile(dataurl: string) {
let arr: string[] = dataurl.split(',');
let bstr = atob(arr[1]);
let n = bstr.length;
let data = new Uint8Array(n);
while(n--){
data[n] = bstr.charCodeAt(n);
}
return data;
}

export async function loadImage(url: string) {
const base64 = await fetchAuto(url);
const img = Canvas.MakeImageFromEncoded(dataURLtoFile(base64));
if(!img) throw new Error("Invalid Image");
return img;
}

export const createCanvas = Canvas.MakeCanvas

export * from "./types.ts"
export default Canvas
Loading

0 comments on commit fbc8cb3

Please sign in to comment.