如何让这个函数对onclick方法做出反应?

时间:2011-11-28 04:19:18

标签: javascript canvas mouseevent

这是按钮,它当前将获取画布图像并在另一个画布上显示该图像。

  var formElement2 = document.getElementById("recImage");
     formElement2.addEventListener('click', recImagePressed, false);

     function recImagePressed(e){
         var outputCanvas = document.getElementById("outputCanvas");
         var recr =  canvas2.toDataURL(); 
         outputCtx = outputCanvas.getContext('2d');
         outputCtx.drawImage(canvas2, 0, 0);
         //context2.clearRect(0, 0, canvas2.width, canvas2.height);<----***
     }  

 //***I need the image to clear when the user clicks, the above is wrong

我需要对onclick做出反应的函数是:(如果我手动将png放入函数中,此函数已经过测试并可以正常工作)

function init () {  <---------this will be done away with and replaced w/ 
                              onClick ?? <-----------------****
canvas = document.getElementById('canVas');  
ctx = canvas.getContext('2d'); 
draw ();
}

function draw() {   <------This is the function that I need to react to mouse event
img = new Image();  
img.src = myPng.png ***---->This is where I need the canvas image<---------------*** 
fr1 = makeFrame(ctx, makeVect(400,0), makeVect(400, 0), makeVect(0, 400));
img.onload = function(){ 
    ctx.save(); 
    newPainter = cornerSplit(imagePainter,5);
    newPainter(fr1);     
    ctx.restore();
    ctx.save();
    newPainter(flipHorizLeft(fr1));
    ctx.restore();
    ctx.save();
    newPainter(flipVertDown(fr1));  
    ctx.restore();
    ctx.save();
    newPainter(flipVertDown(flipHorizLeft(fr1)));   
       }  
} 

1 个答案:

答案 0 :(得分:1)

formElement2.onclick = function(args, ...) {

}