【问题标题】:HTML CSS3 / Canvas Image DistortionHTML CSS3 / 画布图像失真
【发布时间】:2013-05-28 01:11:17
【问题描述】:

有没有办法在 HTML5 中使用 CSS3 或 canvas 标签只倾斜/扭曲一个角?

这是 Photoshop 教程的截图:

更新:

这是迄今为止我发现的最好的,但不是 100% 准确: https://github.com/edankwan/PerspectiveTransform.js

更新2:

我需要这个的 html5 版本: http://www.rubenswieringa.com/code/as3/flex/DistortImage/

【问题讨论】:

  • +1 我认为这是一个非常有趣的问题 :) 我找到了一些关于 do it with Kinetics.js 的信息(您可能以前找到过,但如果有帮助的话)。

标签: html css canvas distortion


【解决方案1】:

这应该对您有所帮助。 Link1

您应该在发布问题之前尝试搜索。我搜索了 html5 canvas skew image,它显示了很多结果。


更新
看看这个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);
});

【讨论】:

【解决方案2】:

我认为this 是您正在寻找的。​​p>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    • 2013-07-25
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多