Problem loading URL

1

I tried to place an image on canvas and here everything is fine:

 var hats = {
      santa: new Image()
    }

    hats.santa.src = "https://1.bp.blogspot.com/-TLqidoaU-g4/V1UwLG1dWvI/AAAAAAAAC34/4RpuUek_09UwHRsb7HEnaFs8j8OiEhZAwCLcB/s320/pvPMlO2.png";

    if (this.nameSkin.indexOf('pluses') !== -1) {
      mainCtx.save();
      mainCtx.globalAlpha = 1;
      //mainCtx.drawImage(hats.santa, this.x - Math.max(this.size * .19, this._nameSize * .8), this.y - Math.max(this.size * .30, this._nameSize * .8), 0.47 * this.size, 0.47 * this.size);
      mainCtx.drawImage(hats.santa, this.x - this.size, this.y - this.size, 2 * this.size, 2 * this.size);
      drawText(this.x - Math.max(this.size * .0, this._nameSize * .0), this.y - Math.max(this.size * .05), '101', this._nameSize * 0.5, false);
      mainCtx.restore();
    }

Then I tried that the src of the holy image, by a url sent by the user, doing the following:

function recibirUrl() {

var urlcompleta = document.getElementById('url').value;

 var hats = {
      santa: new Image()
    }
hats.santa.src = urlcompleta;



}

IN HTML:

<input type="text" id="url">
<input type="submit" onClick="recibirUrl()">

But before all this, the image does not appear to me, I do not know what the error is, I'm a novice in javascript, thanks!

    
asked by Eduardo Sebastian 17.04.2017 в 22:37
source

1 answer

1

Dear Eduardo, you put a button to send the form but before being sent you execute the function recibirUrl() , which tries to load the image but all this happens so quick refresh the page (by sending the form) that does not give time to see the new image, I recommend you do the following:

Change your HTML code as follows:

<input type="text" id="url" onChange="recibirUrl()">
<input type="submit">
    
answered by 18.04.2017 в 03:24