Domanda

I want to create canvas-based site menu using this plugin, to create 'multiply' effect. However this, and also globalCompositeOperation, are working on ctx objects in the same canvas. (in context-blender it uses an off-screen canvas and uses its draw info, globalcompoperation blends the same ctx). I want to create mouse events (hover and click) for each ctx object, so each ctx will lead to a different url.

Here is my test:

      function draw(){
      var ctx = document.getElementById('canvasOff1').getContext('2d');
      var ctx2 = document.getElementById('canvasReal').getContext('2d');
      var ctx3 = document.getElementById('canvasOff3').getContext('2d');

      // draw circles - each circle should link to different url and has its own focus
      ctx.fillStyle = "#c7302a";
      ctx.beginPath();
      ctx.arc(50,75,35,0,Math.PI*2,true);
      ctx.fill();

      ctx2.fillStyle = "#395797";
      ctx2.beginPath();
      ctx2.arc(100,75,35,0,Math.PI*2,true);
      ctx2.fill();

  ctx3.fillStyle = "#454";
      ctx3.beginPath();
      ctx3.arc(150,75,35,0,Math.PI*2,true);
      ctx3.fill();

    var over = canvasOff1.getContext('2d'); 
    var under = canvasReal.getContext('2d');
    over.blendOnto(under,'multiply');

    var over2 = canvasOff3.getContext('2d'); 
    var under2 = canvasReal.getContext('2d');
    over2.blendOnto(under2,'multiply',{destX:0,destY:0});
    }

Would be nice to know though how Can I implement jQuery here. Thanks.

È stato utile?

Soluzione

You can't add event listeners to a context, only to the canvas:

document.getElementById('canvasOff1').addEventLsitener(
    'click',
    function(){ goToUrl('http://www.test1.com'); }
);
document.getElementById('canvasReal').addEventLsitener(
    'click',
    function(){ goToUrl('http://www.test2.com'); }
);
document.getElementById('canvasOff3').addEventLsitener(
    'click',
    function(){ goToUrl('http://www.test3.com'); }
);

function goToUrl(url){
    window.location = url;
}

Or, with jQuery:

$('#canvasOff1').on(
    'click',
    function(){ goToUrl('http://www.test1.com'); }
);
$('#canvasReal').on(
    'click',
    function(){ goToUrl('http://www.test2.com'); }
);
$('#canvasOff3').on(
    'click',
    function(){ goToUrl('http://www.test3.com'); }
);

function goToUrl(url){
    window.location = url;
}

(I prefer using a separate function for window.location = X, but of course, you couls also use it in the onclick function, like this:

function(){ window.location = 'http://www.test1.com'; }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top