N
N
Np1029012017-05-03 00:59:53
JavaScript
Np102901, 2017-05-03 00:59:53

Why can't I load an image into canvas?

Can't pass image to canvas. I don't understand what the problem is. Works one time.
Here is the code:

<input id="upLoad" type="file">
<img id="snap" src="" alt="" />
<canvas id="canvas"></canvas>

var image = document.querySelector('img#snap'),
    canvas = document.querySelector('canvas'),
    context = canvas.getContext('2d');

$('#upLoad').on('change', function () {

   var file = this.files[0];

   console.log(file);

   var reader = new FileReader();

    reader.onload = function () {

        image.src = event.target.result;

       canvas.width = image.width;
       canvas.height = image.height;

       context.drawImage(image,0,0,canvas.width,canvas.height);
    };


    reader.readAsDataURL(file);
});

CodePen : https://codepen.io/anon/pen/ybXRBr

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
svetlov97, 2017-05-03
@svetlov97

Looked at CodePen everything works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question