Skip to content

Canvas acquired via createCanvasForNode is broken #3885

@microDimensions

Description

@microDimensions

Hello,

When working with the v2.0.0-beta.1 version of fabric on the server side, the node canvas acquired via createCanvasForNode does not seem to behave like a node canvas acquired via "new Canvas", although createCanvasForNode seemingly creates a valid node canvas via "new Canvas" as well. Below is the sample SERVER SIDE code required to re-create the issue

Notes:
Using our own built version of node-canvas, since the pre-built one conflicts with sharp image processing library.
OS: Windows 7
Node version: 7.9

Here is the file used for the test case:
honey_im_subtle

let Canvas = require('canvas');
let fabric = require('fabric').fabric;
fabric.canvasModule = 'canvas';

var fs = require('fs');

fs.readFile("honey_im_subtle.png", function (err, data) {
    if (err) {
        return console.log(err);
    }

    let fabric_canvas = fabric.createCanvasForNode(179, 132);

    let node_canvas = fabric_canvas.nodeCanvas;

    // using a newly created canvas works
    //let node_canvas = new Canvas(179, 132);

    let background_img = new Canvas.Image;
    background_img.src = data;

    let ctx = node_canvas.getContext('2d');
    ctx.drawImage(background_img, 0, 0, 179, 132); // fails

    // ensure the image was drawn to canvas
    let out = fs.createWriteStream(__dirname + '/canvas.png');
    let stream = node_canvas.pngStream();

    stream.on('data', function (chunk) {
        out.write(chunk);
    });

    stream.on('end', function () {
        console.log('saved canvas');
    });
});

The error message reads "Image or Canvas expected". I am attempting now to access the node canvas directly only because the following two attempts fail with the exact same error:

  • Setting a background image on the fabric canvas via SetBackgroundImage
  • Adding an image to the fabric canvas via "add"

Any help would be greatly appreciated!

Thomas

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions