如何在paperJS中用图像填充矩形

How to fill a rectangle with an image in paperJS?

本文关键字:图像 填充矩形 paperJS      更新时间:2023-09-26

我需要用图像填充一个矩形。我尝试过光栅,但我无法弄清楚如何在画布上创建的矩形内使用光栅。

是否有类似于 fillColor() 方法的函数来用图像而不是颜色填充矩形?

任何提示/技巧或样品小提琴都会很棒!

以下是您可以执行的操作:

// Create your raster:
var url = 'http://assets.paperjs.org/images/marilyn.jpg';
var raster = new Raster(url);
raster.position = new Point(300,300);
// Use clipMask to create a custom polygon clip mask:
var path = new Path.Rectangle(150,150,100,150);
path.clipMask = true;
// It is better to add the path and the raster in a group (but not mandatory)
/*
var group = new Group();
group.addChild(raster);
group.addChild(path);
*/

// If you just need a rectangle part of a raster, you could use getSubRaster(rect) instead:
/*
var subRaster = raster.getSubRaster(new Rectangle(150,150,100,150));
subRaster.position = new Point(600,600);
*/