Creating an Image Zoomer in HTML5 Canvas.
Step 1. HTML
Here are html code of our color picker page
index.html
| 04 |
<meta charset=”utf-8″ /> |
| 05 |
<title>HTML5 canvas – Image zoomer | Script Tutorials</title> |
| 06 |
<link href=”css/main.css” rel=”stylesheet” type=”text/css” /> |
| 07 |
<script type=”text/javascript” src=”js/jquery-1.5.2.min.js”></script> |
| 08 |
<script type=”text/javascript” src=”js/script.js”></script> |
| 11 |
<div class=”container”> |
| 12 |
<canvas id=”panel” width=”800″ height=”533″></canvas> |
| 15 |
<h2>HTML5 canvas – Image zoomer</h2> |
Step 2. CSS
Here are used CSS styles
css/main.css
| 07 |
background-color:#bababa; |
| 09 |
font:14px/1.3 Arial,sans-serif; |
| 13 |
background-color:#212121; |
| 15 |
box-shadow: 0 -1px 2px #111111; |
| 34 |
footer a.stuts,a.stuts:visited{ |
| 41 |
margin:23px 0 0 110px; |
| 60 |
border:1px #000 solid; |
| 61 |
box-shadow:4px 6px 6px #444444; |
Step 3. JS
js/script.js
| 04 |
var iMouseX, iMouseY = 1; |
| 05 |
var bMouseDown = false; |
| 06 |
var iZoomRadius = 100; |
| 10 |
function clear() { // clear canvas function |
| 11 |
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); |
| 14 |
function drawScene() { // main drawScene function |
| 15 |
clear(); // clear canvas |
| 17 |
if (bMouseDown) { // drawing zoom area |
| 18 |
ctx.drawImage(image, 0 – iMouseX * (iZoomPower – 1), 0 – iMouseY * (iZoomPower – 1), ctx.canvas.width * iZoomPower, ctx.canvas.height * iZoomPower); |
| 19 |
ctx.globalCompositeOperation = ‘destination-atop’; |
| 21 |
var oGrd = ctx.createRadialGradient(iMouseX, iMouseY, 0, iMouseX, iMouseY, iZoomRadius); |
| 22 |
oGrd.addColorStop(0.8, “rgba(0, 0, 0, 1.0)”); |
| 23 |
oGrd.addColorStop(1.0, “rgba(0, 0, 0, 0.1)”); |
| 26 |
ctx.arc(iMouseX, iMouseY, iZoomRadius, 0, Math.PI*2, true); |
| 32 |
ctx.drawImage(image, 0, 0, ctx.canvas.width, ctx.canvas.height); |
| 36 |
// loading source image |
| 38 |
image.onload = function () { |
| 40 |
image.src = ‘images/image.jpg’; |
| 42 |
// creating canvas object |
| 43 |
canvas = document.getElementById(’panel’); |
| 44 |
ctx = canvas.getContext(’2d’); |
| 46 |
$(’#panel’).mousemove(function(e) { // mouse move handler |
| 47 |
var canvasOffset = $(canvas).offset(); |
| 48 |
iMouseX = Math.floor(e.pageX – canvasOffset.left); |
| 49 |
iMouseY = Math.floor(e.pageY – canvasOffset.top); |
| 52 |
$(’#panel’).mousedown(function(e) { // binding mousedown event |
| 56 |
$(’#panel’).mouseup(function(e) { // binding mouseup event |
| 60 |
setInterval(drawScene, 30); // loop drawScene |
What I doing: When we holding the mouse button and start moving it – variable bMouseDown become three. Then, in the main draw function we just going to check this variable and, in the case of the true, we’re just going to draw a new additional area with enlarged image (in a circle). After – pay attention to using of ‘globalCompositeOperation’. Using this method existing canvas is only kept where it overlaps the new shape. The new shape is drawn behind the canvas content.
Downlod live package view live demo
Wonderful piece I have never understood how they do this Enjoyed it
Best Wishes
stevetheblogger