html背景设置形状,html5 – 图像作为绘制形状的“背景”

您可以通过定义与您的形状相同的

clipping region,然后使用drawImage()绘制到此区域来执行此操作;然后中风(只)你的路径在此之上。

以下是相关代码;它按比例缩放图像以填充您指定的宽度:

function clippedBackgroundImage( ctx, img, w, h ){

ctx.save(); // Save the context before clipping

ctx.clip(); // Clip to whatever path is on the context

var imgHeight = w / img.width * img.height;

if (imgHeight < h){

ctx.fillStyle = '#000';

ctx.fill();

}

ctx.drawImage(img,0,0,w,imgHeight);

ctx.restore(); // Get rid of the clipping region

}

如果你想要平铺,不对称拉伸,低透明度着色等等,你可以修改它。这是你可以如何使用它:

function slashedRectWithBG( ctx, x, y, w, h, slash, img ){

ctx.save(); // Save the context before we muck up its properties

ctx.translate(x,y);

ctx.beginPath();

ctx.moveTo( slash, 0 );

ctx.lineTo( w, 0 ); // //

ctx.lineTo( w, h-slash ); // //

ctx.lineTo( w-slash,h ); // //

ctx.lineTo( 0, h ); // //

ctx.lineTo( 0, slash );

ctx.closePath();

clippedBackgroundImage( ctx, img, w, h );

ctx.stroke(); // Now draw our path

ctx.restore(); // Put the canvas back how it was before we started

}

var img = new Image;

img.onload = function(){

// Now you can pass the `img` object to various functions

};

img.src = "...";

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值