我正在使用 Paper.js 在图像上绘图、添加文本等。然后我需要在图像上绘制所有草图并将其全部发送回服务器。具体来说,我在路径光栅化方面遇到了麻烦。我正在尝试使用
paper.project.layers[0].rasterize();
但是当我在图像上这样做时,我没有让线条光栅化。我最终得到
data:,
而不是以“data:image/png;base64”为前缀的 base64 编码图像。这是我在其中工作的 Paper.js草图。要使用它,请在小猫周围单击几下以绘制几条线。一旦你有两条线,应该会打开一个新窗口,显示带有你绘制的红线的光栅化图像。
它适用于草图,但不适用于我自己的代码。
这是我管理绘图的 React 类:
var DrawingTools = React.createClass({
componentDidUpdate: function() {
// Initial path object, will be reset for new paths after Alt is released
var path = this.newPath();
// On mousedown add point to start from
paper.project.layers[0].on('mousedown', function(event) {
if(event.event.altKey && !event.event.ctrlKey) { // Alt key to add a path, but disallow attempting to add text at the same time
if(path.lastSegment == null) {
path.add(event.point, event.point);
} else {
path.add(path.lastSegment.point, path.lastSegment.point)
}
}
});
// On mousedrag add points to path
paper.project.layers[0].on('mousedrag', function(event) {
if(event.event.altKey && !event.event.ctrlKey) { // Alt key to add a path, but disallow attempting to add text at the same time
if(event.event.shiftKey) { // Use shift key for freeform
path.add(event.point);
} else { // Default of straight line added to path
path.lastSegment.point = event.point;
}
}
}.bind(this));
// Each time Alt comes up, start a new path
var tool = new paper.Tool();
tool.onKeyUp = function(event) {
if(event.key == "option") {
path.onMouseEnter = this.props.movableEvents.showSelected;
path.onMouseDrag = this.props.movableEvents.dragItem;
path.onMouseLeave = this.props.movableEvents.hideSelected;
// Start a new path
path = this.newPath();
}
}.bind(this);
},
newPath: function() {
var path = new paper.Path();
path.strokeColor = 'black';
path.strokeWidth = 10;
return path;
},
render: function() {
// Someday colors will go here, or thickness controls, etc.
return (
<div>
</div>
);
}
});
module.exports = DrawingTools;
这是进行光栅化的代码:
var layerAsRaster = paper.project.layers[0].rasterize(); // TODO flatten layers when have multiple layers // Layer to Paper.js Raster object
var dataString = layerAsRaster.toDataURL();
console.log(dataString);
所以光栅化适用于添加PointTexts,但不适用于路径。为什么是这样?与草图相比,我的代码有什么问题?