Topic: Tilt-Shift photo effect in Canvas (Page 1 of 1) |
|
---|---|
Paranoid (IV) Inmate From: Norway |
posted 01-10-2009 12:54
Oy! Turns out I opened my mouth when we were talking about Tilt-Shift photo effect at work and my fellow WebApp team devs challenged me to implement such effect in Canvas. |
Paranoid (IV) Inmate From: Florida |
posted 01-10-2009 23:03
Take that, unbelievers! |
Nervous Wreck (II) Inmate From: Portland Oregon |
posted 01-10-2009 23:22
Very nice. |
Paranoid (IV) Inmate From: Norway |
posted 01-11-2009 21:35
racerX: Ben, "Mr. Joffe", is a cool guy. I've worked, and partied, with him. I really doubt he'd have any problem as your code is not a total ripoff of his. |
Nervous Wreck (II) Inmate From: Portland Oregon |
posted 01-11-2009 23:45
OK here's the code. I picked out the parts of his script that deal with the image and the points. |
Nervous Wreck (II) Inmate From: Portland Oregon |
posted 01-11-2009 23:48
|
Nervous Wreck (II) Inmate From: Portland Oregon |
posted 01-11-2009 23:52
uh oh, the script got screwed up above code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <HTML lang="en"> <HEAD> <TITLE>HTML</title> <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <META name="Content-Script-Type" content="text/javascript"> <META name="Content-Style-Type" content="text/css"> <STYLE type="text/css"> <!-- BODY {margin: 0;padding: 0;background-color: #000;color: #FFF;font-family: arial;font-size: 10pt;line-height: 200%;} CANVAS {background-color: #FFF;} --> </STYLE> <SCRIPT type="text/javascript"> <!-- const Refinement=1//sets size of slices less for better refinement const x=100 const y=200 var pic=new Image(); pic.src="http://www.ozoneasylum.com/images/layouts/ozoneasylum/logos/default.jpg" CanvasRenderingContext2D.prototype.skew=function(src, co){ this.save();var height=src.height;var width=src.width;var cr8ctx=document.createElement('CANVAS').getContext('2d'); var leftSpace=Math.min(co[0][0], co[2][0]);//topleftX or bottmleftX return smallest var totalWidth=Math.max(co[1][0], co[3][0])-leftSpace;// top right or bottom right return biggest - smallest cr8ctx.canvas.setAttribute('width', totalWidth);//biggest-smallest cr8ctx.canvas.setAttribute('height', height);// actual image height if (co[2][0]<co[0][0]) cr8ctx.translate(co[0][0]-co[2][0], 0);// if BL<TL translate(TL-BL,0) var topWidth=co[1][0]-co[0][0];//TR-TL var botWidth=co[3][0]-co[2][0];//BR-BL var leftChange=co[2][0]-co[0][0];//BL-TL for (var i=0; i<height; i+=Refinement) cr8ctx.drawImage(src, 0, i, width, Refinement, leftChange*i/height, i, Math.abs((topWidth*(height-i)+botWidth*i)/height), Refinement); var leftTop=co[0][1]-(co[1][1]-co[0][1])*(co[0][0]-leftSpace)/(co[1][0]-co[0][0]); var rightTop=co[1][1]+(co[1][1]-co[0][1])*(leftSpace+totalWidth-co[1][0])/(co[1][0]-co[0][0]); var leftBot=co[2][1]-(co[3][1]-co[2][1])*(co[2][0]-leftSpace)/(co[3][0]-co[2][0])-leftTop; var rightBot=co[3][1]+(co[3][1]-co[2][1])*(leftSpace+totalWidth-co[3][0])/(co[3][0]-co[2][0])-rightTop; for (var i=0; i<totalWidth; i+=Refinement) this.drawImage(cr8ctx.canvas, i, 0, Refinement, height, leftSpace+i, (leftTop*(totalWidth-i)+rightTop*i)/totalWidth, Refinement, (leftBot*(totalWidth-i)+rightBot*i)/totalWidth); this.restore(); } function skewpic(dot){ var canvas=document.getElementById('canvas').getContext('2d'); for (var i=0; i<4; i++) {canvas.skew(pic, dot);} } // //Number.prototype.sqr=function(){return this*this}; |0 [0][0]=p0x 1 [1][0]=p1x // topleft topright bottomleft bottomright | [0][1]=p0y [1][1]=p1y //THIS SCRIPT ADAPTED FROM BEN JOFFES'S CANVAS SKEW. PLEASE RETAIN THIS NOTICE. THANK(YOU) //var dot=[[30,30],[380,100],[130,280],[180,80]]; |2 [2][0]=p2x 3 [3][0]=p3x // | [2][1]=p2x [3][1]=p3x //--> </SCRIPT> </HEAD> <BODY onload="skewpic([[30,240],[440,180],[200,460],[540,260]])"> <CANVAS id="canvas" width="800" height="500"></CANVAS><BR> <B>Canvas Skew</B> <a href="javascript:skewpic([[30,70],[380,10],[200,460],[280,180]])">hey</a> </BODY> </HTML> |
Nervous Wreck (II) Inmate From: Portland Oregon |
posted 01-12-2009 21:31
hehe figured it out. There's no reason to be looping canvas.skew(pic,dot). Duhhh. |
Paranoid (IV) Inmate From: Norway |
posted 01-12-2009 21:46
I just optimized the Tilt-Shift a little. Now I only generate the most blurry image and combine it with the original, thus saving in memory and dividing the number of drawImage for the actual render by 2 |
Neurotic (0) Inmate Newly admitted From: |
posted 02-18-2009 14:07
Hey poi, |
Paranoid (IV) Inmate From: Norway |
posted 02-18-2009 16:39
benjoffe: mmh ... good point about the gradient + globalCompositeOperation. There's got to be a way to use them and update the output in real time. |
Neurotic (0) Inmate Newly admitted From: |
posted 07-30-2010 11:03
Hi racerx, |
Nervous Wreck (II) Inmate From: |
posted 05-31-2011 11:03
Edit TP: spam removed
|
Obsessive-Compulsive (I) Inmate From: |
posted 03-12-2012 14:31
I want to do something using some of the methods above, but much simpler I beliew... I simply want to take an image and skew it so that it looks like it has 3-dimensional perspective to it. You can see an example here of what I am trying to accomplish here: |