The second time of onchage does not work on an input file

0

The code tries to draw on the canvas the image that I select with the input, but it turns out that in the code I have, the first time I enter an image it works but when I re-select another photo in the input file, the onchange no longer works.

HTML Code:

 <canvas id="cv01" onclick="document.getElementById('imgn').click();"> </canvas>
<footer>
    <button onclick="document.getElementById('imgn').click();">Seleccionar imagen</button>
    <input type="file" name="img" id="imgn" accept="image/*" onchange="imagen(); cambiarBotones();"> <br>
</footer>

<canvas id="cv02"></canvas>
<footer>
    <label>Dificultad: </label>
    <select id="dificultad" onchange="lineas();">
        <option value="facil">Fácil</option>
        <option value="medio">Medio</option>
        <option value="dificil">Difícil</option>
      </select>
      <input type="color" id="color" onchange="lineas();">
      <button disabled="disabled" id="start" onclick="empezar();">Empezar</button>
      <button disabled="disabled" id="end" onclick="terminar();">Terminar juego</button>
      <button disabled="disabled" id="ayuda" >Ayuda</button>
      <div id='seconds-counter'> </div>

</footer>

JS Code:

  function imagen(){
let cv = document.querySelector('#cv01'),
    cv02 = document.querySelector('#cv02'),
    ctx = cv.getContext('2d'),
    ctx2 = cv02.getContext('2d'),
    img=new Image(); //img es un objeto de js

imagen= document.getElementById("imgn");
var curFile = imagen.files[0];
img.src = window.URL.createObjectURL(curFile);

img.onload = function(){
    //ctx.drawImage(img,0,0);
    ctx.drawImage(img,0,0, cv.width, cv.height); //Para que tenga el tamano maximo del canvas
    ctx2.drawImage(img,0,0,cv01.width, cv01.height);
    lineas();
}
_IMG_=img;
//imagen.value = "";  limpiar el input }



function lineas(){
    let cv = document.querySelector('#cv02'),
    ctx = cv.getContext('2d'),
    inp = document.getElementById('dificultad').value,
    color = document.getElementById('color').value;


if(inp =='facil'){
    r=3;
}
else if(inp =='medio'){
    r=6;
}
else if(inp == 'dificil'){
    r=8;
}
piezaCanvasHeight=_ALTO_/r;
piezaCanvasWidth = _ANCHO_/r;
let dimv = cv.width/r,
    dimh = cv.height/r;

ctx.clearRect(0, 0,360,240);//Reiniciar canvas
copiar();   //Copiar la imagen del canvas 1 

//Empezar a dibujar lineas
ctx.beginPath();
ctx.lineWidth=2;
ctx.strokeStyle=color;

    for(let i=0; i<=r; i++){
        //lineas verticales
        ctx.moveTo(i*dimv,0);
        ctx.lineTo(i*dimv,cv.height);
        //lineas horizontales
        ctx.moveTo(0,i*dimh);
        ctx.lineTo(cv.width,i*dimh);
        ctx.stroke();
    }

}

function cambiarBotones(){
    start.disabled=false;
    start.style.background="rgba(255, 255, 0)";
}

This is the error that comes to me in the console:

  

Uncaught TypeError: image is not a function       at HTMLInputElement.onchange ((index): 24)

Thank you!

    
asked by Kora 08.06.2018 в 11:32
source

1 answer

2

The error is in the function itself:

function imagen(){
  let cv = document.querySelector('#cv01'),
    cv02 = document.querySelector('#cv02'),
    ctx = cv.getContext('2d'),
    ctx2 = cv02.getContext('2d'),
    img=new Image(); //img es un objeto de js

  imagen= document.getElementById("imgn");
    ...
}

You define an image function: correct, although it is not a good name, the verb is missing (what the function does).

Within the function you define a group of variables with let , but you make a failure: after img there is a semicolon ( ; ), so that image is not a local variable, but the function per se. So after executing the function for the first time, image ceases to be a function to be the value of document.getElementById("imgn") , which is not a function.

The solution is to remove the semicolon and put a comma, but apart you could:

  • rename the function to something more descriptive.
  • add 'use strict'; to the beginning of the same so that the compiler can detect possible similar errors.
answered by 08.06.2018 / 12:50
source