Skip to content

loading images from URL to draw on canvas #561

Closed

Description

@shiffman

I'm working on an example that uses createImg() to load an image from a URL to draw on canvas. The solution is to hide the DOM element, i.e.

var img;

function setup() {
  createCanvas(640, 360);
  img = createImg("http://processing.org/img/processing-web.png", test);
  img.hide();
}


function draw() {
  background(0);
  image(img, 0, 0);
}

However, this doesn't work b/c the img element's width and height are 0,0. This fixes it:

function draw() {
  background(0);
  image(img, 0, 0, img.elt.width, img.elt.height);
}

Three questions:

  1. This seems like a bug as one should be able to say image(img, 0, 0); and draw the image at its native pixel width and height.
  2. Is this something we want to consider making happen behind the scenes if a user calls loadImage() on a URL or is that hiding too much?
  3. It feels like an optional callback for createImg() would be useful to know when the image data is loaded and ready.

Thoughts?

Activity

  1. lmccart commented on Apr 9, 2015

    @lmccart
    Member

    interesting... I ran into this issue the other day and agree it would be nice. let's start tagging all DOM related ideas with the "dom" tag so if we have tackle this with GSoC or at CMU we can find them easily.

  2. self-assigned this
    on Jun 18, 2015
  3. added a commit that references this issue on Jun 19, 2015
    9015ffe
  4. eliawk commented on Oct 16, 2016

    @eliawk

    Hello,
    i'm using createImg to load an external image and then i'd like to use the p5.Image method (get, filter,...) but i get a p5.Element instead.

    • p5.Element {elt: img, _pInst: undefined, _events: Object, width: 0, height: 0}

    if i try to force the element to be an image
    cover = new p5.Image(cover_load.width,cover_load.height,cover_load);

    i get this error:
    p5.js:14593 Uncaught IndexSizeError: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The source width is 0.

    where am i wrong?

    thnaks

    E

  5. julsgud commented on Apr 9, 2017

    @julsgud

    Hey All!

    Wondering if this is still a relevant fix worth looking into since loading images from url to canvas is possible when not using p5! (which is always a bummer!)

    This stack overflow thread has the following example of how it can be achieved.

    var c = document.getElementById("myCanvas");
    var ctx = c.getContext("2d");
    var img = new Image();
    img.onload = function() { ctx.drawImage(img, 0, 0); };
    img.src = 'https://cdn.sstatic.net/stackexchange/img/logos/so/so-icon.png';

    Thoughts?

    Happy sketching :)

  6. andreiverse commented on May 7, 2018

    @andreiverse

    I love when I am looking for CanvasJS and I see this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions