gpt4 book ai didi

canvas - 剪辑到绘制的路径

转载 作者:行者123 更新时间:2023-12-04 12:40:13 25 4
gpt4 key购买 nike

我正在尝试绘制一条路径,他们将其用作我 Canvas 的蒙版。

'use strict';

var canvas = new fabric.Canvas('c', {
hoverCursor: 'pointer',
isDrawingMode: true
});

canvas.freeDrawingBrush = new fabric.PencilBrush(canvas);
canvas.freeDrawingBrush.color = '#000';
canvas.freeDrawingBrush.width = 100;

fabric.Image.fromURL('http://fabricjs.com/assets/pug_small.jpg', function(img) {

canvas.add(img);
canvas.setWidth(img.getWidth());
canvas.setHeight(img.getHeight());
canvas.centerObject(img);
img.selectable = false;
});

canvas.on('path:created', function(data) {

var path = data.path;

canvas.remove(path);
canvas.clipTo = function(context) {
path.render(context);
};

canvas.isDrawingMode = false;
canvas.renderAll();
});

如何使整个路径成为图像的可见区域?

http://jsfiddle.net/db45yhpo/

编辑

这就是我想要实现的目标,但使用的是 FabricJS。

http://www.createjs.com/demos/easeljs/alphamaskreveal

最佳答案

我没有fabricjs的经验,所以可能有更好的处理方法,但我会:
在隐藏的 Canvas 上重绘你的路径,
然后从这个隐藏的 Canvas 创建一个新的 fabric.Image(),
设置其globalCompositeOperation参数为`"destination-atop",
在原始 Canvas 上绘制它。

var canvas = new fabric.Canvas('c', {
hoverCursor: 'pointer',
isDrawingMode: true
});

canvas.freeDrawingBrush = new fabric.PencilBrush(canvas);
canvas.freeDrawingBrush.color = '#000';
canvas.freeDrawingBrush.width = 100;

fabric.Image.fromURL('http://fabricjs.com/assets/pug_small.jpg', function(img) {
canvas.add(img);
canvas.setWidth(img.getWidth());
canvas.setHeight(img.getHeight());
canvas.centerObject(img);
img.selectable = false;
});

canvas.on('path:created', function(data) {
var clipper = document.createElement('canvas');
clipper.width = canvas.width;
clipper.height = canvas.height;
var ctx = clipper.getContext('2d');
var path = data.path;
data.path.render(ctx);
canvas.remove(path);
canvas.isDrawingMode = false;
var oImg = new fabric.Image(clipper)
oImg.globalCompositeOperation = 'destination-atop';
canvas.add(oImg);
canvas.renderAll();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.5.0/fabric.min.js"></script>
<canvas id="c"></canvas>

关于canvas - 剪辑到绘制的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33110872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com