Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canvas Clear in Paper.js

Does anyone know the best way to clear a canvas using paper.js I tried the regular canvas clearing methods but they do not seem to work with 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>
like image 610
craig Avatar asked Sep 27 '13 15:09

craig


People also ask

How do you clear a canvas element?

To clear the Canvas, you can use the clearRect() method. This method performs pretty well than others for clearing the canvas (such as resetting the width/height, destroying the canvas element and then recreating it, etc..) const context = canvas. getContext('2d'); context.

How do you erase a picture on canvas?

To clear the HTML5 Canvas, we can use the clearRect() method to clear the canvas bitmap. This performs much better than other techniques for clearing the canvas, such as resetting the canvas width and height, or destroying the canvas element and then recreating it.

What is paper JS used for?

Paper. js is a JavaScript library for drawings and animations. It's based largely on Scriptographer, a scripting language for Adobe Illustrator. You can write JavaScript with Paper.


2 Answers

Regular clearing does not work because paper.js maintains a scene graph and takes care of drawing it for you. If you want to clear all items that you have created in a project, you need to delete the actual items:

project.activeLayer.removeChildren(); works as long as all your items are inside one layer. There is also project.clear() which removes everything, including the layer.

like image 95
Jürg Lehni Avatar answered Sep 26 '22 15:09

Jürg Lehni


In case someone comes looking for an answer with little experience in Javascript, I made a simple example :

1) As Jürg Lehni mentioned project.activeLayer.removeChildren(); can be used to remove all items inside active layer. 2) To reflect the cleaning you have to call 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%;
}
like image 45
rohan-patel Avatar answered Sep 24 '22 15:09

rohan-patel