css - 如何在不扭曲图像的情况下获得空中飞人图像?

标签 css image css-shapes

我试图以空中飞人的形式呈现图像,而不会弄乱实际图像,也不会使用倾斜。我该怎么做?

最佳答案

您可以为此使用 Canvas :

From demo

Online demo here

简单调整 HTML 以使用 Canvas 代替:

<div class="box">
    <canvas id="canvas"></canvas>
</div>    

删除一些 CSS 规则规范:

.box{
    height:300px;  
    overflow:hidden;
}

.box > canvas {
    height: 100%;
    width: 100%;
}

并在 JavaScript 部分添加:

var img = new Image,
    ctx = document.getElementById('canvas').getContext('2d');

/// load image dynamically (or use an existing image in DOM)
img.onload = draw;
img.src = 'http://cssglobe.com/lab/angled/img.jpg';

function draw() {

    /// set canvas size = image size
    var w, h;
    canvas.width = w = this.naturalWidth;
    canvas.height = h = this.naturalHeight;

    /// draw the trapez shape
    ctx.moveTo(0, h * 0.25);
    ctx.lineTo(w, 0);
    ctx.lineTo(w, h);
    ctx.lineTo(0, h * 0.75);

    /// make it solid
    ctx.fill();

    /// change composite mode to replace the solid shape
    ctx.globalCompositeOperation = 'source-in';

    /// draw in image
    ctx.drawImage(this, 0, 0);
}

这适用于所有现代浏览器。如果不需要,那么 palpatim 的方法会更快。

希望这对您有所帮助。

关于css - 如何在不扭曲图像的情况下获得空中飞人图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21197337/

相关文章:

css - 内框阴影仅 3 边和三 Angular 形 - 表单进度条

html - 我可以创建一个带有 flex 底部的div吗?

html - Flexbox 100% 高度问题

javascript - 选中复选框时使 div 可见的附加 Javascript 代码

html - CSS - 悬停时的 Spring 侧边菜单

python - 为什么我在尝试将图像分成两半时出现“平铺无法扩展外部图像”错误

javascript - 附加图像并通过 json 以 base64 编码发送

css - 创建不寻常的 CSS3 形状来替换图像

html - 将导航栏链接居中

android - 如何在java中从drawable读取图像到File对象?