somanyerrorswhy
somanyerrorswhy

Reputation: 173

How would I generate a pulsating effect on an image that is to be drawn inside of a canvas?

    var testPhoto = new Image();
    testPhoto.src = "http://plan9.bell-labs.com/plan9/img/9logo.jpg"
    testPhoto.className = "pulse";

and then:

    topSlice.drawImage(testPhoto, 100, 200, 40, 40);

It appears the pulsating effect doesn't work after drawing the image, how should I fix this? I'm following this tutorial for the pulsating effect.

Upvotes: 0

Views: 222

Answers (1)

le_m
le_m

Reputation: 20248

You can use Window.requestAnimationFrame instead and work with blend modes / alpha blending:

var canvas = document.getElementById("canvas"),
    context = canvas.getContext("2d"),
    image = new Image();


image.src = "http://plan9.bell-labs.com/plan9/img/9logo.jpg";

function update(timestamp) {
  
  context.clearRect(0, 0, canvas.width, canvas.height);
  context.globalAlpha = Math.sin(timestamp/100) * 0.5 + 0.5;
  context.drawImage(image, 0, 0);
  
  window.requestAnimationFrame(update);
}

window.requestAnimationFrame(update);
<canvas id="canvas"></canvas>

Upvotes: 2

Related Questions