Paper.js: Raster fails when setting source with DOM Image

Created on 19 Mar 2019  Â·  3Comments  Â·  Source: paperjs/paper.js

Description/Steps to reproduce

I am experiencing an error creating a new Raster object when using a DOM image as the source.
I've noticed that instantiating a Raster in different ways produces different results however:

const domImg = new window.Image(100, 100);
domImg.src = 'http://paperjs.org/about/paper-js.gif';

const ok = new paper.Raster(domImg);
const fails = new paper.Raster({source: domImg});

If I define an onError(error) function on the latter case, I get:

JSON.stringify(error) // "{"event":{"isTrusted":true},"type":"error"}"
error.event.target   // <img src=​"[object HTMLImageElement]​">​

UPDATE: This seems to be a problem with setting Raster.source using a DOM Image.

Link to reproduction test-case


See Sketch: instead of seeing three Raster images, only one succeeds.

Screenshot 2019-03-20 at 3 34 31 PM

Expected result

Image should load in both cases from DOM as specified in the documentation.

Additional information


paper 0.12.0, node v8.12.0, Ubuntu 16.04.5 LTS, Chrome Version 72

documentation feedback bug

Most helpful comment

Yes, the documentation is wrong here. Raster#source is only expected to be a string, and can either be a URL or a data URL. Raster#image accepts a DOM image or a canvas object. I'll change the documentation.

All 3 comments

Hi, thank you for the report.

As a workaround, waiting for a correction, you can use raster.image property instead.
See this sketch for a demonstration.

new Raster({
    image: domImg
});

var raster= new Raster();
raster.image = domImg;

About what we should do, I see 2 possibilities:

  • Changing documentation to make it describe actual behavior: only a URL can be set as the source.
    This seems problematic to me because the getter can return either an image, a canvas or a URL and there would be an inconsistency between getter and setter.
  • Make source setter support either image, canvas or URL as described in the documentation.
    I think this makes more sense as it was the legacy behavior as we can see browsing the source history.
    So the user might expect that.

@lehni, what do you think ?

Yes, the documentation is wrong here. Raster#source is only expected to be a string, and can either be a URL or a data URL. Raster#image accepts a DOM image or a canvas object. I'll change the documentation.

BTW, the constructor itself supports both versions: new Raster(image) and new Raster(source)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sansumbrella picture sansumbrella  Â·  3Comments

s-light picture s-light  Â·  7Comments

przyb picture przyb  Â·  5Comments

kaelumania picture kaelumania  Â·  6Comments

jsbroks picture jsbroks  Â·  4Comments