2013-09-27 54 views
9

有谁知道以清除使用paper.js 我尝试了常规帆布结算方法画布的最佳方式,但他们似乎不paper.js帆布清除在Paper.js

工作的Html

<canvas id="myCanvas" style="background:url(images/graphpaper.jpg); background-repeat:no-repeat" height="400px" width="400px;"></canvas> 

<button class="btn btn-default button" id="clearcanvas" onClick="clearcanvas();"  type="button">Clear</button>  

的JavaScript/Paperscript

<script type="text/paperscript" canvas="myCanvas"> 
// Create a Paper.js Path to draw a line into it: 
tool.minDistance = 5; 

var path; 
var c=document.getElementById("myCanvas"); 
var ctx=c.getContext("2d"); 

function onMouseDown(event) { 
// Create a new path and give it a stroke color: 
path = new Path(); 
path.strokeColor = 'red'; 
path.strokeWidth= 3; 
path.opacity="0.4"; 

// Add a segment to the path where 
// you clicked: 
path.add(event.point, event.point); 
} 

function onMouseDrag(event) { 
// Every drag event, add a segment 
// to the path at the position of the mouse: 
path.add(event.point, event.point); 

} 


</script> 

回答

1

c.width = c.width; ctx.clearRect(0,0,c.width,c.height);应该是一个很好的包罗万象的,如果你还没有尝试过。

但是要小心 - 设置画布宽度将重置画布状态,包括所有应用的变换。

快速谷歌把我带到https://groups.google.com/forum/#!topic/paperjs/orL7YwBdZq4指定另一个(更具体paper.js)解决方案: project.activeLayer.removeChildren();

+0

你描述上面的命令清除帆布,而不是场景图。然而,第二个工作得很好。 –

15

定期结算不起作用,因为paper.js维持场景图和图纸需要为您的护理。如果要清除在项目中创建的所有项目,则需要删除实际项目:

project.activeLayer.removeChildren();只要所有项目都位于一个图层内即可使用。还有project.clear()可以删除所有内容,包括图层。

3

万一有人来寻找在JavaScript中缺乏经验的答案,我做了一个简单的例子:作为尔格Lehni提到project.activeLayer.removeChildren();可以用来去除活性层内的所有物品

1)。 2)要反映清洁,你必须拨打paper.view.draw();

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF-8"> 
    <title>Circles</title> 
    <link rel="stylesheet" href="style.css"> 
    <script type="text/javascript" src="paper-full.js"></script> 
     <script type="text/paperscript" canvas="canvas"> 

function onMouseUp(event) { 
    var circle = new Path.Circle({ 
    center: event.middlePoint, 
    radius: event.delta.length/2 
}); 
circle.strokeColor = 'black'; 
circle.fillColor = 'white'; 

} 

</script> 

<script type="text/javascript"> 
    paper.install(window); 
    window.onload = function() { 
     paper.setup('canvas'); 
     document.getElementById('reset').onclick = function(){ 

      paper.project.activeLayer.removeChildren(); 
      paper.view.draw(); 

     } 

    }; 

    </script>  
</head> 
<body> 
    <canvas style="background-color: #eeeeed" id="canvas" resize></canvas> 
    <input id="reset" type="button" value="Reset"/> 
</body> 
</html> 

CSS:

html, 
body { 
    margin: 0; 
    overflow: hidden; 
    height: 100%; 
} 

/* Scale canvas with resize attribute to full size */ 
canvas[resize] { 
    width: 58%; 
    height: 100%; 
}