首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用画布更改对象形状的背景

使用画布更改对象形状的背景
EN

Stack Overflow用户
提问于 2013-12-31 07:50:30
回答 1查看 82关注 0票数 0

我想实现一个服装设计网站,但我遇到了一个问题。

我有一件形象衬衫,一件形象布。我把衬衫画在帆布上。现在,我想换一件的布衬衫。但我不知道该怎么做。

示例:http://sbhvietnam.vn/shirt-by-hand.html

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-31 16:50:18

您可以使用复合模式来实现这一点。如果加载了图像,并且图像后面的背景是透明的,并且设置了画布和上下文(此处为ctx),则可以这样做:

代码语言:javascript
复制
/// clear if you reuse this method to avoid pixelated outline
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);

/// draw shirt shape
ctx.drawImage(imgShirt, 0, 0);

/// change composite mode for next draw
ctx.globalCompositeOperation = 'source-atop';  /// source-in is an option

/// draw cloth image on top and it will take the shape of the shirt image.
ctx.drawImage(imgCloth, 0, 0);

/// reset composite mode to default mode
ctx.globalCompositeOperation = 'source-over';

注意,如果您应该选择使用source-in而不是source-atop,那么每次您想要在其上更改图像时,都必须重新绘制imgShirt

每次你需要重新绘制布料时,你都可以用你调用的函数把它包装起来。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20853860

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档