html - 记录Paper.js路径对象,稍后再重绘

标签 html canvas paperjs

我用鼠标 Paper.js 画画。我需要保留这些笔画并以与视频重播相同的速率重播它们。我怎样才能做到这一点?

最佳答案

在 paper.js 中,onFrame()函数每秒被调用多达 60 次,而 onMouseMove()函数“当鼠标在项目 View 中移动时调用”,并包含鼠标的位置。通过使用这两个函数,您可以存储鼠标 Action 并稍后在位置之间几乎同时重放它们。

var mousePosition = null;
function onMouseMove(event) {
    if (mousePosition != null) {
        var path = new Path();
        path.strokeColor = 'black';
        path.moveTo(mousePosition);
        path.lineTo(event.point);
    }
    mousePosition = event.point;
}

var recordedPositions = [];
var delayFrames = 60;
function onFrame(event) {
    if (mousePosition != null) {
        recordedPositions.push(mousePosition);
        if (recordedPositions.length > delayFrames) {
            var path = new Path();
            path.strokeColor = 'red';
            delayedPositionIndex = recordedPositions.length - delayFrames;
            path.moveTo(recordedPositions[delayedPositionIndex - 1]);
            path.lineTo(recordedPositions[delayedPositionIndex]);
        }
    }
}

我不知道 onFrame() 的计时精度/分辨率/可靠性。或者,您可以像在这个答案中那样使用 javascript 计时事件:How can I use javascript timing to control on mouse stop and on mouse move events

关于html - 记录Paper.js路径对象,稍后再重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10162206/

相关文章:

html - 使用 :hover to display a div

javascript - 如何从轮子中获取 Canvas 的值(value)

javascript - 如何将图像放在圆形对象上?

javascript - destination-out globalCompositeOperation 不是我所期望的

javascript - 使用 Tween.js 使用 Paper.js 缩放栅格

html - IE10 上的悬停效果

javascript - 平滑向下滚动

javascript - 如何将paper.js应用到简单的html页面?

javascript - 纸.js : can't set fillColor of a symbol instance

html - 自下而上的 CSS 墙