【问题标题】:how to fill a image with pattern如何用图案填充图像
【发布时间】:2013-07-27 12:03:33
【问题描述】:

假设我有一张图片!

现在我想用 填充该图像

我的最终图像应该是这样的

怎么做? 到目前为止,我能够更改该图像的颜色,但无法填充图案。

我可以使用 html5 画布(模式)吗?有没有办法用 php 或任何网络平台来做到这一点。

【问题讨论】:

  • 你不能轻易做到这一点,但如果你想这样做,我不能说任何用于图像处理的库让你的第二张图像有点透明,然后先填充区域图像,所以第一张图像曲线也会显示出来,但你有一点不透明度。你可以使用 magik,gd 用于 php,同样的东西适用于画布,但必须参考其有关归档模式的文档
  • 感谢您的指导。
  • 使用PNGa and CSS
  • 亲爱的@SverriM.Olsen 这是什么“PNGa 和 CSS”。你能提供他们网站的链接吗?谷歌无法为我提供答案。
  • 使用三层可能是最简单的,你有的图片是最低的,中间有不透明度的图案,和衬衫图片形状一样的中心是空的叠加png (透明)提供一种剪切区域。尽管 SVG 有学习曲线,但使用图像作为蒙版创建 SVG 可能会更简单。 W3C on SVG masking

标签: php jquery html html5-canvas fabricjs


【解决方案1】:

使用以下步骤创建模拟,将映射图案应用于您的衬衫:

  • 为您的衬衫打造高对比度版本。
  • 在画布上绘制那件衬衫的图像
  • 将 globalCompositeOperation 设置为“source-atop”
  • (任何新绘图只会出现在衬衫图像不透明的地方)
  • 从您的方格图像创建图案
  • 用方格图案填充画布
  • (只会出现在不透明的衬衫上)
  • 将 globalAlpha 设置为非常低的值
  • 反复绘制高对比度衬衫的图像
  • (这有效地叠加了衬衫的“皱纹”)

为了更好的解决方案

创建衬衫的“凹凸贴图”并在 three.js 中应用方格图案

这是代码和小提琴:http://jsfiddle.net/m1erickson/kzfKD/

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script src="http://code.jquery.com/jquery.min.js"></script>

<style>
    body{ background-color: ivory; }
    canvas{border:1px solid red;}
</style>

<script>
$(function(){

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");

    var img1=new Image();
    var img=new Image();
    img.onload=function(){

        img1.onload=function(){
            start();
        }
        img1.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/4jiSz1.png";
    }
    img.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/BooMu1.png";

    function start(){

        ctx.drawImage(img1,0,0);

        ctx.globalCompositeOperation="source-atop";

        ctx.globalAlpha=.85;

        var pattern = ctx.createPattern(img, 'repeat');
        ctx.rect(0, 0, canvas.width, canvas.height);
        ctx.fillStyle = pattern;
        ctx.fill();

        ctx.globalAlpha=.15;
        ctx.drawImage(img1,0,0);
        ctx.drawImage(img1,0,0);

    }


}); // end $(function(){});
</script>

</head>

<body>
    <canvas id="canvas" width=436 height=567></canvas>
</body>
</html>

【讨论】:

  • 顺便问一下,你是如何创建衬衫的“凹凸图”的?我可以用 jquery、html5 做还是我需要回到 photoshop?
  • 是的,在photoshop中创建纹理贴图(凹凸贴图)。
  • 纹理贴图和凹凸贴图是两个不同的东西。对于 2D displacement 贴图更适合,对于 3D 法线贴图和作为辅助凹凸贴图。
【解决方案2】:

正如您问题的 cmets 中所建议的那样,一种方法是覆盖 DOM 元素——顶部的 DOM 元素应该是具有透明度的 PNG,而底部的元素应该是您的背景图案。这也有效(而且速度更快,因为您不必计算组合图像),但在图像组合方式方面提供的灵活性稍差。使用 canvas 方法,你可以使用任何你想要的混合模式。

大多数浏览器尚不支持的第二个选项是使用CSS background blend modes。这将允许您创建具有透明度的 PNG 图像,为其分配背景颜色,并使用与 CSS 的混合。这速度很快,并且只需要一个 DOM 元素。

第三种方法是使用画布。 编辑:ma​​rkE 的画布方法更快、更简单。) 我在这个 JSFiddle 中实现了一种基于画布的方法:http://jsfiddle.net/IceCreamYou/uzzLa/——这是要点:

// Get the base image data
var image_data = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
var image_data_array = image_data.data;

// Get the pattern image data
var overlay_data = ovlyCtx.getImageData(0, 0, ovlyCtx.canvas.width, ovlyCtx.canvas.height).data;

// Loop over the pixels in the base image and merge the colors
for (var i = 0, j = image_data_array.length; i < j; i+=4) {
    // Only merge when the base image pixel is nontransparent
    // Alternatively you could implement a border-checking algorithm depending on your needs
    if (image_data_array[i+3] > 0) {
        image_data_array[i+0] = combine(image_data_array[i+0], overlay_data[i+0]); // r
        image_data_array[i+1] = combine(image_data_array[i+1], overlay_data[i+1]); // g
        image_data_array[i+2] = combine(image_data_array[i+2], overlay_data[i+2]); // b
    }
}

// Write the image data back to the canvas
ctx.putImageData(image_data, 0, 0);

它的作用是创建一个带有基础图像的画布和第二个平铺图案图像的画布,然后在基础像素不透明时使用像素数据将图案覆盖在基础上。

【讨论】:

    猜你喜欢
    • 2015-06-03
    • 1970-01-01
    • 2015-04-17
    • 2013-09-06
    • 2015-12-02
    • 2020-05-01
    • 1970-01-01
    • 2020-05-22
    • 2013-12-19
    相关资源
    最近更新 更多