【发布时间】:2019-10-27 18:09:39
【问题描述】:
假设我有一张图片!
现在我想用下面的织物填充该图像。
- 我的最终图像将如下所示
怎么做?到目前为止,我能够更改该图像的颜色,但无法填充图案。
有没有办法用 PHP、jquery 和 javascript 来做呢。
提前致谢!
演示链接:https://www.bombayshirts.com/custom/shirt
我只想像上面的演示那样在衬衫上填充图案。
我尝试过的是下面的,但是使用下面的代码,我失去了我的衬衫形状并且图像会被像素化。
var img1 = new Image, img2 = new Image, cnt = 2,
canvas = document.getElementById("canvas"),
ctx = canvas.getContext("2d");
img1.onload = img2.onload = function() {if (!--cnt) go()};
img1.src = "https://www.itailor.co.uk/images/product/shirt/it1823-1.png"; // Shirt
img2.src = "https://i.stack.imgur.com/sQlu8.png"; // pattern
// MAIN CODE ---
function go() {
// create a pattern
ctx.fillStyle = ctx.createPattern(img2, "repeat");
// fill canvas with pattern
ctx.fillRect(0, 0, canvas.width, canvas.height);
// use blending mode multiply
ctx.globalCompositeOperation = "multiply";
// draw sofa on top
ctx.drawImage(img1, 0, 0, img1.width*.5, img1.height*.5);
// change composition mode
ctx.globalCompositeOperation = "destination-in";
// draw to cut-out sofa
ctx.drawImage(img1, 0, 0, img1.width*.5, img1.height*.5);
}
<canvas id="canvas" width=500 height=300></canvas>
【问题讨论】:
-
我认为你可以用 svg 模式来做到这一点......但只交换图像会容易得多。如果有其他解决方案,我想看看,听起来很有趣。
-
是的,这很有趣@maximelian1986
-
:-) 你想让它遵循袖子的逻辑形状吗?可以(在某些浏览器中)根据亮度进行一些基本的像素位移,但这就是 2D 上下文能够为您提供的所有内容。你愿意做的意味着一个 3D 模型。并在画布上制作图案:developer.mozilla.org/en-US/docs/Web/API/…
-
谢谢@Kaiido,我想按照上面的截图填充衬衫上的面料。我已经准备好使用 2D 或 3D 了,但我只是想在衬衫上填充织物图案而不是像素化。
-
这是标记为 jquery 的?!?为什么??如果没有赏金,它会因为过于广泛而被关闭。
标签: javascript jquery image-processing canvas 3d