Paper.js栅格化层失败

时间:2015-10-15 19:32:18

标签: javascript raster paperjs rasterizing

我使用Paper.js绘制图像,添加文本等。然后我需要对图像进行所有草图绘制并将其全部发送回服务器。具体来说,我在路径栅格化方面遇到了麻烦。我试图使用

paper.project.layers[0].rasterize();

但是当我在图像上执行此操作时,我没有让线条进行栅格化。我最终得到了

data:,

而不是带有类似" data:image / png; base64,"等前缀的base64编码图像。这是一个Paper.js sketch我在这里工作。要使用它,请在kitty周围单击几次以绘制几行。一旦有两行,就会打开一个新窗口,显示带有红线的光栅化图像。

它适用于草图,但不适用于我自己的代码。

这是管理绘图的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,但不适用于路径。为什么是这样?我的代码与草图相反有什么问题?

1 个答案:

答案 0 :(得分:1)

我认为你的问题是错误的。也许更好的方法是保存用户点击的点,然后将数据发送回服务器,你知道它们将始终形成直线。稍微优化网络访问。

相关问题