14

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/ペーパースクリプト

<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>
4

4 に答える 4

22

paper.js がシーン グラフを維持し、描画を処理するため、通常のクリアは機能しません。プロジェクトで作成したすべてのアイテムをクリアする場合は、実際のアイテムを削除する必要があります。

project.activeLayer.removeChildren();すべてのアイテムが 1 つのレイヤー内にある限り機能します。project.clear()レイヤーを含むすべてを削除するものもあります。

于 2013-10-10T10:43:15.473 に答える
4

Javascript の経験がほとんどない人が答えを探しに来た場合に備えて、簡単な例を作成しました。

1)JürgLehniが述べproject.activeLayer.removeChildren();たように、アクティブレイヤー内のすべてのアイテムを削除するために使用できます。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%;
}
于 2015-05-08T08:13:41.387 に答える
1

レイヤー全体をクリアするために使用project.activeLayer.removeChildren();するか、新しいパスをグループに追加してグループ内のすべての子を削除することができます

var myPath;
var group = new Group();

function onMouseDown(event) {
    myPath = new Path();
}

function onMouseDrag(event) {
    myPath.add(event.point);
}

function onMouseUp(event) {

    var myCircle = new Path.Circle({
        center: event.point,
        radius: 10
    });
    myCircle.strokeColor = 'black';
    myCircle.fillColor = 'red';
    group.addChild(myCircle);
}


    // connect your undo function and button
    document.getElementById('clearbutton').onclick = btnClear;

    function btnClear(){
        group.removeChildren();
    }
于 2019-03-06T08:19:24.033 に答える