Posts

Showing posts with the label event handler

Execute Canvas rendering based on scroll event.

Image
I have been showing animations using canvas in several blog posts lately and implemented a lot of this in my own homepage. Now, if you are doing animations you need to plan how the animations should execute so that the user does not miss the whole show. :) You might want to kick off the animation on a click, or even on a long page – start the animation once the user scrolls down to the animations position. There would be little use in an animation if the show were over when the user gets to it, right? Now, you can hook on to the browsers events using the window object or the document object. The click event I’ve already covered in this post . For the scroll event you need to grab the window object. The easiest way to do this is to use JQuery and extend the scroll event. In this example I’ve added a delay before kicking of the animation. I’ve also calculated the position the user should scroll to before firing the event handler. var target = $("#mainSectionFootprint...

Create an interactive HTML5 Canvas using event handlers.

Image
You can use HTML5 Canvas as an alternative approach designing info graphics, branding graphics etc. Using the example in my previous blog post, I will add a clickable area on the canvas and use a JavaScript event handler to execute some logic. This is the result of the previous example. I want to pop an alert Box when clicking inside the logo. I need to create an object reflecting the coordinates I want my clickable zone to have. I’m also throwing in an id/name. var CustFigure = function (x, y, height, width, name ) { this.x = x; this.y = y; this.height = height; this.width = width; this.name = name; }; In this case I just create a global Array that I can pop the objects on to.   var custFigures = []; At some point (I choose to include it in the function drawing  the logo) we create the object with the desired coordinates and push it on to the array. var custFigure = new CustFigure(50, 50, 75+50, 75+50, " Hello ...