2013-05-06 107 views
3

有沒有辦法在HTML5中使用CSS3或canvas標籤歪斜/扭曲只有一個角?HTML CSS3/Canvas Image Distortion

這裏是從Photoshop教程截圖如何做到這一點:

Skew in Photoshop

更新:

這是到目前爲止,我已經找到了最好的,但它不是100%準確: https://github.com/edankwan/PerspectiveTransform.js

UPDATE2:

我需要這個HTML5版本: http://www.rubenswieringa.com/code/as3/flex/DistortImage/

+0

+1我認爲這是一個非常有趣的問題:)我找到了一些信息(你可能會發現它,但在有幫助的情況下)關於[與Kinetics.js一起使用](http://codeslashslashcomment.com/2012/12/12 /動態圖像失真HTML5的帆布/)。 – Arkana 2013-05-06 14:38:12

回答

1

這會幫助你。 Link1

而您應該在發佈問題前嘗試搜索。我搜索了html5帆布歪斜圖像,它顯示了我很多結果。


更新
看看這個 Fiddle

// Find each img, and replace it with a canvas 
$('img').each(function (index, el) { 
var c,  // new canvas which will replace this img element 
    ctx, // context of new canvas 
    i,  // loop counter 
    tmpCtx, // temp context for doing work 
    h,  // height of the image/new canvas 
    w,  // width of the image/new canvas 
    dh,  // destination height (used in translation) 
    dw,  // destination width (used in translation) 
    dy,  // destination y 
    leftTop,// left top corner position 
    leftBot;// left bottom corner position 

// Get the height/width of the image 
h = el.height; 
w = el.width; 

// Create the canvas and context that will replace the image 
c = $("<canvas height='" + h + "' width='" + w + "'><\/canvas>"); 
ctx = c.get(0).getContext('2d'); 

// Create a temporary work area 
tmpCtx = document.createElement('canvas').getContext('2d'); 

// Draw the image on the temp work area 
for (i = 0; i < h; i++) { 
    dw = Math.abs((w * (h - i) + w * i)/h); 
    tmpCtx.drawImage(el, 
     0, i, w, 1, // sx, sy, sw, sh 
     0, i, dw, 1); // dx, dy, dw, dh 
} 

// Calculate the left corners to be 20% of the height 
leftTop = parseInt(h * .2, 10); 
leftBot = parseInt(h * 1, 10) - leftTop; 

ctx.save(); 

// Draw the image on our real canvas 
for (i = 0; i < w; i++) { 
    dy = (leftTop * (w - i))/w; 
    dh = (leftBot * (w - i) + h * i)/w; 
    ctx.drawImage(tmpCtx.canvas, 
     i, 0, 1, h, 
     i, dy, 1, dh); 
} 

ctx.restore(); 

// Replace the image with the canvas version 
$(el).replaceWith(c); 
}); 
+0

感謝您的鏈接,但這些鏈接並沒有幫助我。正如你看到所有的例子是「正常」的歪斜教程,像這樣做:http://pe-images.s3.amazonaws.com/basics/free-transform/distort-side.gif 或玩透視: http: //pe-images.s3.amazonaws.com/basics/free-transform/perspective-top-left.gif 我需要能夠歪斜只有一個角落 – Hese 2013-05-06 11:55:13

+0

感謝您的更新,但仍然是同樣的問題 – Hese 2013-05-06 13:22:44