### Install Paper.js with jsdom and Canvas Source: https://github.com/paperjs/paper.js/blob/develop/README.md Installs the Paper.js module with jsdom and Canvas rendering capabilities after native dependencies have been successfully installed. ```shell npm install paper-jsdom-canvas ``` -------------------------------- ### Install Build Essentials on Debian/Ubuntu Source: https://github.com/paperjs/paper.js/blob/develop/README.md Installs the 'build-essential' package on Debian/Ubuntu systems, which is required for compiling C++ sources and is a prerequisite for some Paper.js dependencies. ```shell sudo apt-get install build-essential ``` -------------------------------- ### Install Paper.js Development Dependencies (Yarn) Source: https://github.com/paperjs/paper.js/blob/develop/README.md Installs the NPM dependencies required for developing and building Paper.js using Yarn, after cloning the repository. ```shell yarn install ``` -------------------------------- ### Install Native Dependencies on Debian/Ubuntu Linux Source: https://github.com/paperjs/paper.js/blob/develop/README.md Installs necessary development packages for Paper.js on Debian/Ubuntu systems, including Cairo, Pango, SSL, JPEG, and GIF development headers. ```shell sudo apt-get install pkg-config libcairo2-dev libpango1.0-dev libssl-dev libjpeg62-dev libgif-dev ``` -------------------------------- ### Paper.js Initialization and Setup Source: https://github.com/paperjs/paper.js/blob/develop/examples/Rasters/Raster.html This snippet demonstrates the basic setup for Paper.js, including initializing the PaperScope and creating a canvas element. It's essential for any Paper.js application. ```javascript var canvas = document.getElementById('myCanvas'); paper.setup(canvas); ``` -------------------------------- ### Install Native Dependencies on macOS (Homebrew) Source: https://github.com/paperjs/paper.js/blob/develop/README.md Installs Cairo and Pango, essential for Node-Canvas rendering in Paper.js, using Homebrew. This is the primary method for macOS installations. ```shell brew install cairo pango ``` -------------------------------- ### Install Dependencies with npm Source: https://github.com/paperjs/paper.js/blob/develop/examples/Rasters/Raster.html Installs project dependencies using npm. This command is essential for setting up the development environment and ensuring all necessary libraries are available. ```bash npm install ``` -------------------------------- ### Install Paper.js via NPM Source: https://github.com/paperjs/paper.js/blob/develop/README.md This command installs the Paper.js library as a project dependency using the Node Package Manager (NPM). It is the recommended method for projects running in browsers, Node.js, or Electron. ```sh npm install paper ``` -------------------------------- ### Clone Paper.js Repository Source: https://github.com/paperjs/paper.js/blob/develop/README.md Clones the Paper.js source code repository from GitHub, including all submodules, and navigates into the cloned directory. ```shell git clone --recursive git://github.com/paperjs/paper.js.git cd paper.js ``` -------------------------------- ### Create and Style a Path with Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/JSON/Raster.html This snippet demonstrates how to create a basic path in Paper.js, define its segments, and apply styling such as stroke color and width. It's a fundamental example for getting started with vector graphics in Paper.js. ```javascript var path = new Path(); path.strokeColor = 'black'; path.add(new Point(100, 100)); path.add(new Point(200, 200)); path.strokeWidth = 10; ``` -------------------------------- ### Load Paper.js from Source Files Source: https://github.com/paperjs/paper.js/blob/develop/README.md Switches Paper.js to load directly from source files using the 'load' Gulp task and Prepro.js. This is useful for development and testing without a full build. ```shell yarn load ``` -------------------------------- ### Configure PKG_CONFIG_PATH for macOS Source: https://github.com/paperjs/paper.js/blob/develop/README.md Adds the PKG_CONFIG_PATH export to the .bash_profile for persistent configuration of Pango and Cairo dependencies on macOS. This simplifies subsequent install and update commands. ```shell # PKG Config for Pango / Cairo export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/opt/X11/lib/pkgconfig ``` -------------------------------- ### Fetch Origin Changes for Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/README.md Fetches the latest changes from the 'origin' remote repository for Paper.js without merging them into the local branch. ```shell git fetch origin ``` -------------------------------- ### Install Paper.js with macOS Cairo/Pango Fix Source: https://github.com/paperjs/paper.js/blob/develop/README.md Installs Paper.js on macOS when encountering errors with Cairo, by setting the PKG_CONFIG_PATH environment variable. This ensures correct dependency resolution. ```shell PKG_CONFIG_PATH=/opt/X11/lib/pkgconfig/ npm install paper ``` -------------------------------- ### Basic PaperScript Setup with Canvas Source: https://github.com/paperjs/paper.js/blob/develop/examples/JSON/Raster.html Provides a minimal PaperScript setup, including linking to the Paper.js library and defining PaperScript code within a ` ``` -------------------------------- ### Update jsdoc-toolkit Submodule Source: https://github.com/paperjs/paper.js/blob/develop/README.md Initializes and updates the 'jsdoc-toolkit' submodule within the Paper.js repository, which is used for generating documentation. ```shell git submodule update --init --recursive ``` -------------------------------- ### Build Paper.js Library Source: https://github.com/paperjs/paper.js/blob/develop/README.md Compiles the Paper.js source files into distributable format using the 'build' Gulp task. The compiled files will be located in the 'dist' folder. ```shell yarn build ``` -------------------------------- ### Create Zipped Paper.js Distribution Source: https://github.com/paperjs/paper.js/blob/develop/README.md Creates a final zipped distribution file for Paper.js within the 'dist' folder using the 'dist' Gulp task. ```shell yarn dist ``` -------------------------------- ### Update Paper.js on macOS with Fix Source: https://github.com/paperjs/paper.js/blob/develop/README.md Updates Paper.js on macOS while maintaining the fix for Cairo/Pango dependency issues by specifying the PKG_CONFIG_PATH. ```shell PKG_CONFIG_PATH=/opt/X11/lib/pkgconfig/ npm update ``` -------------------------------- ### Basic Paper.js Setup with HTML Canvas Source: https://github.com/paperjs/paper.js/blob/develop/examples/Rasters/Raster.html Shows the fundamental HTML structure required to use Paper.js, including a canvas element and the script tag to include the Paper.js library and your custom script. ```html Paper.js Example ``` -------------------------------- ### Basic Path Manipulation in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Scripts/BlendModes.html This example demonstrates fundamental path manipulation in Paper.js, such as moving and scaling. It starts with a predefined path and then applies transformations. Note that transformations are cumulative if applied sequentially without resetting. ```javascript var path = new Path.Star(new Point(200, 200), 5, 40, 20); path.fillColor = 'orange'; // Move the path path.position.x += 50; // Scale the path path.scale(1.5); ``` -------------------------------- ### Web Audio API Setup for Paper.js Visualizer Source: https://github.com/paperjs/paper.js/blob/develop/examples/Paperjs.org/SatieLikedToDraw.html Sets up the Web Audio API context, audio source, and analyzers for processing audio data in the Paper.js visualizer. It creates two analyzers for left and right channels and prepares a buffer for frequency data. This requires a browser environment supporting Web Audio API. ```javascript var AudioContext = window.AudioContext || window.webkitAudioContext; if (AudioContext) { audio = new AudioContext(); source = audio.createBufferSource(); // Create two separate analyzers for left and right channel. analyserL = audio.createAnalyser(); analyserL.smoothingTimeConstant = 0.25; analyserL.fftSize = Math.pow(2, amount) * 2; analyserR = audio.createAnalyser(); analyserR.smoothingTimeConstant = analyserL.smoothingTimeConstant; analyserR.fftSize = analyserL.fftSize; // Create the buffer to receive the analyzed data. freqByteData = new Uint8Array(analyserL.frequencyBinCount); // Create a splitter to feed them both var splitter = audio.createChannelSplitter(); // Connect audio processing graph source.connect(splitter); splitter.connect(analyserL, 0, 0); splitter.connect(analyserR, 1, 0); // Connect source to output also so we can hear it source.connect(audio.destination); loadAudioBuffer('http://assets.paperjs.org/audio/gnossienne.mp3'); } else { // TODO: Print error message alert('Audio not supported'); } ``` -------------------------------- ### Creating a Rectangle Path in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Scripts/BlendModes.html This code example shows how to define a rectangular path using Paper.js. It takes a starting point and dimensions to create the rectangle. The resulting path can be styled and manipulated further. ```javascript var paper = require('paper'); // Assuming 'paper.setup' has been called with a canvas element var rect = new paper.Path.Rectangle(new paper.Point(50, 50), new paper.Size(100, 75)); rect.strokeColor = 'blue'; rect.strokeWidth = 2; console.log('Rectangle path created.'); ``` -------------------------------- ### Heart Path Animation Setup (JavaScript) Source: https://github.com/paperjs/paper.js/blob/develop/examples/Paperjs.org/Tadpoles.html Initializes Paper.js, defines a heart path using SVG path data, creates an array of boids, and sets up event handlers for frame updates, window resizing, mouse clicks, and key presses to control the simulation. ```javascript var heartPath = new Path('M514.69629,624.70313c-7.10205,-27.02441 -17.2373,-52.39453 -30.40576,-76.10059c-13.17383,-23.70703 -38.65137,-60.52246 -76.44434,-110.45801c-27.71631,-36.64355 -44.78174,-59.89355 -51.19189,-69.74414c-10.5376,-16.02979 -18.15527,-30.74951 -22.84717,-44.14893c-4.69727,-13.39893 -7.04297,-26.97021 -7.04297,-40.71289c0,-25.42432 8.47119,-46.72559 25.42383,-63.90381c16.94775,-17.17871 37.90527,-25.76758 62.87354,-25.76758c25.19287,0 47.06885,8.93262 65.62158,26.79834c13.96826,13.28662 25.30615,33.10059 34.01318,59.4375c7.55859,-25.88037 18.20898,-45.57666 31.95215,-59.09424c19.00879,-18.32178 40.99707,-27.48535 65.96484,-27.48535c24.7373,0 45.69531,8.53564 62.87305,25.5957c17.17871,17.06592 25.76855,37.39551 25.76855,60.98389c0,20.61377 -5.04102,42.08691 -15.11719,64.41895c-10.08203,22.33203 -29.54687,51.59521 -58.40723,87.78271c-37.56738,47.41211 -64.93457,86.35352 -82.11328,116.8125c-13.51758,24.0498 -23.82422,49.24902 -30.9209,75.58594z'); var boids = []; var groupTogether = false; // Add the boids: for (var i = 0; i < 30; i++) { var position = Point.random() * view.size; boids.push(new Boid(position, 10, 0.05)); } function onFrame(event) { for (var i = 0, l = boids.length; i < l; i++) { if (groupTogether) { var length = ((i + event.count / 30) % l) / l * heartPath.length; var point = heartPath.getPointAt(length); if (point) boids[i].arrive(point); } boids[i].run(boids); } } // Reposition the heart path whenever the window is resized: function onResize(event) { heartPath.fitBounds(view.bounds); heartPath.scale(0.8); } function onMouseDown(event) { groupTogether = !groupTogether; } function onKeyDown(event) { if (event.key == 'space') { var layer = project.activeLayer; layer.selected = !layer.selected; return false; } } body { background: black; } ``` -------------------------------- ### Creating and Styling a Rectangle in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Scripts/BlendModes.html Demonstrates the creation and styling of a rectangle. A new Path.Rectangle object is instantiated with a starting point and dimensions. The snippet also shows how to set the fill and stroke colors of the rectangle. ```javascript var rect = new Path.Rectangle({ point: [50, 50], size: [100, 75] }); rect.fillColor = 'green'; rect.strokeColor = 'black'; rect.strokeWidth = 2; ``` -------------------------------- ### Paper.js Initial Path Definition Source: https://github.com/paperjs/paper.js/blob/develop/examples/SVG Export/Tiger.html This snippet represents the beginning of a Paper.js path definition. It includes a starting point and some initial curve commands but is not a complete path. It is associated with a light brown fill color. ```javascript ["Group",{"children":[["Path",{"pathData":"M9.5,-9.88c0,0 -3.04,-20.14 -0.76,-24.32c0,0 10.26,-9.5 9.88,-12.92c0,0 -0.38,-17.1 -1.52,-17.86z","fillColor":["rgb",0.94902,0.72157,0.57255]}]]}] ``` -------------------------------- ### Audio Buffer Loading and Playback Source: https://github.com/paperjs/paper.js/blob/develop/examples/Paperjs.org/SatieLikedToDraw.html Handles asynchronous loading of an MP3 audio file using XMLHttpRequest and decoding it using the Web Audio API. Upon successful loading and decoding, it sets the audio buffer to the source, loops it, starts playback, and resumes the Paper.js view animation. It includes error handling for loading and decoding failures. ```javascript function loadAudioBuffer(url) { // Load asynchronously var request = new XMLHttpRequest(); request.open("GET", url, true); request.responseType = "arraybuffer"; request.onload = function() { audio.decodeAudioData(request.response, function(buffer) { source.buffer = buffer; source.loop = true; source.start(0); view.play(); }, function(buffer) { alert("Error loading MP3"); }); }; request.send(); } ``` -------------------------------- ### Drawing a Path with Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Scripts/BlendModes.html This snippet demonstrates how to draw a basic path using Paper.js. It defines a starting point and adds segments to create a curved line. No external dependencies are required beyond the Paper.js library itself. ```javascript var path = new Path(); path.strokeColor = 'black'; path.add(new Point(100, 100)); path.add(new Point(200, 200)); path.add(new Point(100, 300)); path.add(new Point(200, 400)); ``` -------------------------------- ### Paper.js Audio Visualizer Initialization and Animation Source: https://github.com/paperjs/paper.js/blob/develop/examples/Paperjs.org/SatieLikedToDraw.html Initializes Paper.js paths, groups, and animation loop for audio visualization. It sets up paths for left and right audio channels, defines animation steps, and handles frame updates to reflect audio frequency data. Dependencies include Paper.js and Web Audio API. ```javascript var leftPath = new Path({ strokeColor: 'red', opacity: 0.5 }); var rightPath = new Path({ strokeColor: 'green', opacity: 0.5 }); var amount = 8; var step = view.size.width / (amount + 1); var flip = false; for (var i = 0; i <= amount; i++) { leftPath.add(new Point(i * step, 0)); rightPath.add(new Point(i * step, 0)); } var group = new Group({ children: [leftPath, rightPath], applyMatrix: false, strokeWidth: 30, strokeJoin: 'round', strokeCap: 'butt', pivot: leftPath.position, position: view.center }); function onMouseDown() { flip = !flip; } function onKeyDown(event) { if (event.key === 'space') { group.fullySelected = !group.fullySelected; } } var audio, source, analyserL, analyserR, freqByteData; view.onFrame = function() { var step = view.size.width / (amount + 1); var scale = view.size.height / 1.5; analyserL.getByteFrequencyData(freqByteData); var leftBands = getEqualizerBands(freqByteData, true); analyserR.getByteFrequencyData(freqByteData); var rightBands = getEqualizerBands(freqByteData, true); for (var i = 1; i <= amount; i++) { leftPath.segments[i].point = [i * step, -leftBands[i - 1] * scale]; rightPath.segments[i].point = [i * step, -rightBands[i - 1] * scale * (flip ? -1 : 1)]; } leftPath.smooth(); rightPath.smooth(); group.pivot = [leftPath.position.x, 0]; group.position = view.center; }; // Pause animation until we have data view.pause(); ``` -------------------------------- ### Creating an Arc with Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Paperjs.org/SpiralRaster.html This example demonstrates the creation of an arc segment in Paper.js. It specifies the bounding rectangle, start angle, and end angle for the arc. ```javascript var arc = new Path.Arc(new Point(50, 50), new Point(150, 50), new Point(100, 150)); arc.strokeColor = 'orange'; ``` -------------------------------- ### Create an Arc in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Rasters/Raster.html This example shows how to create an arc segment for a path in Paper.js. An arc is defined by its start point, end point, and the radius of the circle it belongs to. ```javascript var arc = new Path(); arc.add(new Point(100, 100)); arc.addArc(new Segment(new Point(200, 100), new Point(200, 100), new Point(200, 100), new Point(200, 100), new Size(50, 50), 0, 180)); arc.strokeColor = 'blue'; ``` -------------------------------- ### Create an Arc with Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/JSON/Raster.html This example demonstrates how to create an arc segment in Paper.js. It defines the start point, end point, and radius to draw a portion of a circle or ellipse, useful for curved lines and shapes. ```javascript var center = new Point(150, 150); var start = new Point(150, 100); var end = new Point(200, 150); var arc = new Path.Arc(center, start, end); arc.strokeColor = 'red'; arc.strokeWidth = 5; ``` -------------------------------- ### Add a Click Event to a Path in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/JSON/Raster.html This example shows how to attach a click event listener to a Paper.js path. When the path is clicked, an alert box will appear. This requires a Paper.js project setup with an event handling context. ```javascript var clickableRect = new Path.Rectangle(50, 50, 100, 100); clickableRect.fillColor = 'purple'; clickableRect.onClick = function(event) { alert('You clicked the purple rectangle!'); }; ``` -------------------------------- ### Draw a Bezier Curve in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Rasters/Raster.html This example demonstrates how to draw a cubic Bezier curve using Paper.js. It defines the start point, end point, and two control points for the curve. The Paper.js library needs to be included. ```javascript var start = new Point(50, 50); var end = new Point(200, 50); var control1 = new Point(50, 150); var control2 = new Point(200, 150); var curve = new Path.CubicBezier(start, control1, control2, end); curve.setStrokeColor('purple'); ``` -------------------------------- ### Drawing a Bezier Curve in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/JSON/Raster.html This example demonstrates how to draw a cubic Bezier curve using Paper.js. It involves defining the start point, end point, and two control points. The `Path.Bezier` constructor creates the curve, which can then be styled. ```javascript var start = new Point(50, 300); var end = new Point(200, 350); var control1 = new Point(80, 250); var control2 = new Point(150, 400); var curve = new Path.Bezier(start, control1, control2, end); curve.strokeColor = 'orange'; curve.strokeWidth = 2; ``` -------------------------------- ### Handle Mouse Click Events with Paper.js (JavaScript) Source: https://github.com/paperjs/paper.js/blob/develop/examples/JSON/Raster.html Provides an example of how to detect and respond to mouse click events within a Paper.js canvas. This requires the Paper.js library and an event listener setup. It logs the click event's point to the console. ```javascript view.onMouseUp = function(event) { console.log('Mouse clicked at: ' + event.point); var circle = new Path.Circle({ center: event.point, radius: 10, fillColor: 'yellow' }); } ``` -------------------------------- ### Creating and Animating a Simple Path in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Scripts/BlendModes.html This snippet demonstrates the creation of a basic path and its subsequent animation over time. It requires Paper.js to be loaded. ```javascript var path = new Path.Line(new Point(50, 50), new Point(200, 50)); path.strokeColor = 'blue'; path.strokeWidth = 4; view.onFrame = function(event) { path.segments[1].point.x += 1; if (path.segments[1].point.x > 300) { path.segments[1].point.x = 50; } } ``` -------------------------------- ### Creating an Arc Path in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Scripts/BlendModes.html This example shows how to create an arc segment using Paper.js. It defines the bounding rectangle for the arc, the start and end angles, and whether the arc should be counterclockwise. This is useful for pie charts or circular progress indicators. ```javascript var arc = new Path.Arc({ from: [100, 100], to: [200, 100], radius: 50, center: [150, 100], clockwise: false }); arc.strokeColor = 'red'; arc.strokeWidth = 2; ``` -------------------------------- ### Drawing a Circle with Paper.js (JavaScript) Source: https://github.com/paperjs/paper.js/blob/develop/examples/Paperjs.org/Qbertify.html This snippet demonstrates how to create and draw a circle using the Paper.js library in JavaScript. It utilizes the `Path.Circle` constructor to define the circle's properties and adds it to the project's item tree for rendering. No external dependencies beyond Paper.js itself are required. ```javascript var c = new Path.Circle(new Point(100, 100), 50); c.fillColor = 'red'; ``` -------------------------------- ### Draw a Bezier Curve with Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Rasters/Raster.html This example shows how to draw a cubic Bezier curve using Paper.js. It defines the start point, end point, and two control points to shape the curve. Bezier curves are essential for creating smooth, organic shapes. ```javascript var start = new Point(50, 100); var end = new Point(250, 100); var control1 = new Point(100, 0); var control2 = new Point(200, 200); var curve = new Path({ segments: [start, control1, control2, end], strokeColor: 'blue', fullySelected: true }); ``` -------------------------------- ### Draw a Bezier Curve in Paper.js (JavaScript) Source: https://github.com/paperjs/paper.js/blob/develop/examples/Rasters/Smoothing.html This example shows how to draw a cubic Bezier curve using Paper.js. It defines the start point, end point, and two control points to shape the curve. Styling is applied to make the curve visible. ```javascript var startPoint = new Point(50, 100); var endPoint = new Point(200, 100); var controlPoint1 = new Point(80, 50); var controlPoint2 = new Point(170, 150); var curve = new Path({ segments: [ startPoint, { point: controlPoint1, type: 'bezier' }, { point: controlPoint2, type: 'bezier' }, endPoint ], strokeColor: 'red', strokeWidth: 3 }); ``` -------------------------------- ### Initialize and Generate Voronoi Diagram (JavaScript) Source: https://github.com/paperjs/paper.js/blob/develop/examples/Paperjs.org/Voronoi.html Initializes the Voronoi object, generates points for the diagram, and sets up event handlers for mouse and keyboard interactions. It also defines constants for colors and initial view parameters. The `renderDiagram` function is central to updating the visualization. ```javascript var voronoi = new Voronoi(); var sites = generateBeeHivePoints(view.size / 200, true); var bbox, diagram; var oldSize = view.size; var spotColor = new Color('red'); var mousePos = view.center; var selected = false; onResize(); function onMouseDown(event) { sites.push(event.point); renderDiagram(); } function onMouseMove(event) { mousePos = event.point; if (event.count == 0) sites.push(event.point); sites[sites.length - 1] = event.point; renderDiagram(); } function renderDiagram() { project.activeLayer.removeChildren(); var diagram = voronoi.compute(sites, bbox); if (diagram) { for (var i = 0, l = sites.length; i < l; i++) { var cell = diagram.cells[sites[i].voronoiId]; if (cell) { var halfedges = cell.halfedges, length = halfedges.length; if (length > 2) { var points = []; for (var j = 0; j < length; j++) { v = halfedges[j].getEndpoint(); points.push(new Point(v)); } createPath(points, sites[i]); } } } } } function onResize() { var margin = 20; bbox = { xl: margin, xr: view.bounds.width - margin, yt: margin, yb: view.bounds.height - margin }; for (var i = 0, l = sites.length; i < l; i++) { sites[i] = sites[i] * view.size / oldSize; } oldSize = view.size; renderDiagram(); } function onKeyDown(event) { if (event.key == 'space') { selected = !selected; renderDiagram(); } } ``` -------------------------------- ### Draw a Straight Line Path in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/JSON/Raster.html This example illustrates how to draw a straight line path between two points using Paper.js. It defines the start and end points and creates a Path object. The line is then styled with a stroke color. This is a fundamental operation for vector graphics. ```javascript var start = new Point(50, 50); var end = new Point(200, 150); var line = new Path(start, end); line.strokeColor = 'green'; line.strokeWidth = 2; ``` -------------------------------- ### Create and Style a Path in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/SVG Export/Tiger.html This snippet demonstrates how to create a basic path with a specified path data and apply a black fill color using Paper.js. It's a fundamental example for shape creation. ```javascript new Path({ pathData: "M-33.8,129.201c0,0 5.2,-0.4 4,1.2c-1.2,1.6 -3.6,0.8 -3.6,0.8L-33.8,129.201z", fillColor: ["rgb",0,0,0] }) ``` -------------------------------- ### Create and Style a Paper.js Path Source: https://github.com/paperjs/paper.js/blob/develop/examples/Scripts/BlendModes.html This snippet demonstrates how to create a basic path in Paper.js and apply styling such as stroke color and width. It utilizes the `Path.Line` constructor and `strokeColor` and `strokeWidth` properties. ```javascript var start = new Point(100, 100); var end = new Point(200, 200); var line = new Path.Line(start, end); line.strokeColor = 'red'; line.strokeWidth = 4; ``` -------------------------------- ### Create a Bezier Curve Path in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Rasters/Raster.html This code example demonstrates how to create a cubic Bezier curve using Paper.js. It defines the start point, end point, and two control points to shape the curve. Bezier curves are essential for smooth, complex path drawing. ```javascript var start = new Point(50, 150); var end = new Point(250, 150); var control1 = new Point(100, 50); var control2 = new Point(200, 250); var bezierCurve = new Path({ segments: [start, control1, control2, end], strokeColor: 'green', strokeWidth: 4 }); bezierCurve.fullySelected = true; // To see the control points ``` -------------------------------- ### Manage Viewport and Project Structure in Paper.js Source: https://context7.com/paperjs/paper.js/llms.txt Provides examples for accessing and controlling Paper.js view properties like size, center, zoom, and pixel ratio. It also covers managing project layers, styles, and multiple projects. ```javascript // Access view properties console.log('View size:', view.size); console.log('View center:', view.center); console.log('View bounds:', view.bounds); console.log('View zoom:', view.zoom); console.log('Pixel ratio:', view.pixelRatio); // Pan and zoom view.zoom = 2; view.center = new Point(200, 200); view.translate(new Point(50, 0)); view.rotation = 45; // View events view.onResize = function(event) { console.log('New size:', event.size); // Reposition items on resize circle.position = view.center; }; view.onFrame = function(event) { // Global animation handler }; // Force redraw view.draw(); view.requestUpdate(); // Project management console.log('Active layer:', project.activeLayer); console.log('All layers:', project.layers); console.log('Selected items:', project.selectedItems) // Current style for new items project.currentStyle = { fillColor: 'red', strokeColor: 'black', strokeWidth: 2 }; // Deselect all project.deselectAll(); // Clear project project.clear(); // Multiple projects var project1 = new Project(); var project2 = new Project(); project1.activate(); // Create items in project1 project2.activate(); // Create items in project2 // Switch between projects project1.activate(); ``` -------------------------------- ### Creating and manipulating paths in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Paperjs.org/Qbertify.html This example demonstrates how to create a custom path with multiple segments and modify its properties like stroke color and width. It requires the Paper.js library. The code defines points, creates a path, and applies styling. ```javascript var path = new Path(); path.strokeColor = 'black'; path.strokeWidth = 2; path.add(new Point(50, 50)); path.add(new Point(150, 100)); path.add(new Point(100, 150)); ``` -------------------------------- ### Draw a Bezier Curve in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Scripts/BlendModes.html This example shows how to draw a cubic Bezier curve using Paper.js. It defines the start point, end point, and two control points. The resulting curve is then styled with a stroke color and width. This relies on the Paper.js library for path creation. ```javascript var start = new Point(100, 100); var end = new Point(400, 300); var control1 = new Point(150, 300); var control2 = new Point(350, 100); var curve = new Path.Bezier(start, control1, control2, end); curve.strokeColor = 'red'; curve.strokeWidth = 2; ``` -------------------------------- ### Handle Mouse Down Event in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Scripts/BlendModes.html This example demonstrates how to capture and respond to a mouse down event using Paper.js. It sets up an event listener that triggers a function when the mouse button is pressed. This is essential for user interaction within a Paper.js canvas. The code requires a Paper.js project setup with an active canvas. ```javascript var circle = new Path.Circle({ radius: 20, fillcolor: 'blue' }); circle.position = view.center; circle.onMouseDown = function(event) { this.fillColor = 'green'; } ``` -------------------------------- ### Animated Star Creation and Animation in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Animated/AnimatedStar.html This snippet creates multiple star paths with dynamic coloring and smoothing. It then animates the rotation of these stars on each frame and repositions them on window resize. Dependencies include the Paper.js library. ```javascript var layer = project.activeLayer; var values = { count: 34, points: 32 }; for (var i = 0; i < values.count; i++) { var path = new Path({ fillColor: i % 2 ? 'red' : 'black', closed: true }); var offset = new Point(20 + 10 * i, 0); var l = offset.length; for (var j = 0; j < values.points * 2; j++) { offset.angle += 360 / values.points; var vector = offset.normalize(l * (j % 2 ? 0.1 : -0.1)); path.add(offset + vector); } path.smooth({ type: 'continuous' }); layer.insertChild(0, new Group({ children: [path], applyMatrix: false })); } function onFrame(event) { for (var i = 0; i < values.count; i++) { var item = layer.children[i]; var angle = (values.count - i) * Math.sin(event.count / 128) / 10; item.rotate(angle); } } // Reposition the paths whenever the window is resized: function onResize(event) { layer.position = view.center; } ``` -------------------------------- ### Manipulating Paths in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Scripts/BlendModes.html Illustrates how to modify existing paths, including adding segments and changing stroke properties. This code relies on a Paper.js canvas setup. ```javascript var path = new Path(); path.strokeColor = 'black'; path.add(new Point(50, 50)); path.add(new Point(150, 50)); path.add(new Point(150, 150)); path.add(new Point(50, 150)); path.closed = true; path.segments[2].point = new Point(200, 100); path.strokeWidth = 3; ``` -------------------------------- ### Creating Text Elements with Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Scripts/BlendModes.html Illustrates how to create and style text elements using Paper.js. This includes setting the content, font size, and color. Useful for adding labels or information to graphics. ```javascript var textItem = new PointText(new Point(100, 300)); textItem.content = 'Hello, Paper.js!'; textItem.fillColor = 'purple'; textItem.fontSize = 20; ``` -------------------------------- ### Applying Filters to Paths in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Rasters/RotationRaster.html Shows how to apply graphical filters, such as blur, to Paper.js paths. This example applies a Gaussian blur to a rectangle. It requires Paper.js and a canvas setup. ```javascript var rect = new Path.Rectangle({ width: 100, height: 100, fillColor: 'cyan' }); rect.position = view.center; // Apply a blur filter rect.applyMatrix = false; // Important for filters rect.filters = [ new Blur({ blur: 5 }) ]; ``` -------------------------------- ### Paper.js Text Item Creation in JavaScript Source: https://github.com/paperjs/paper.js/blob/develop/examples/Rasters/Raster.html Shows how to create and style text items in Paper.js. This includes setting the content, font, size, and color of the text. ```javascript var text = new PointText(new Point(100, 300)); text.content = 'Hello, Paper.js!'; text.fillColor = 'purple'; text.fontFamily = 'Arial'; text.fontSize = 20; ``` -------------------------------- ### Draw Bezier Curves in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Rasters/Raster.html This snippet demonstrates how to draw a cubic Bezier curve using Paper.js. It defines the start point, end point, and control points for the curve. ```javascript var curve = new Path(); curve.addCubicCurve(new Point(50, 50), new Point(150, 50), new Point(50, 150), new Point(150, 150)); curve.strokeColor = 'green'; curve.strokeWidth = 2; ``` -------------------------------- ### Paper.js: Symbol Placement and Initialization Source: https://github.com/paperjs/paper.js/blob/develop/examples/Animated/SpaceUsingShapes.html This code snippet initializes a Paper.js project by setting up basic project styles and then procedurally generates a specified number of Shape.Circle symbols. Each circle's position, scale, and initial random vector are determined, preparing them for animation. Dependencies include the Paper.js library. ```javascript var count = 150; project.currentStyle = { fillColor: 'white' }; // Place the instances of the symbol: for (var i = 0; i < count; i++) { // The center position is a random point in the view: var center = Point.random() * view.size; var scale = (i + 1) / count; var path = new Shape.Circle(center, 5 * scale); path.data.vector = new Point({ angle: Math.random() * 360, length : scale * Math.random() / 5 }); } ``` -------------------------------- ### Paper.js: Draw a rectangle Source: https://github.com/paperjs/paper.js/blob/develop/examples/JSON/Raster.html This snippet shows how to create a rectangle using Paper.js. It uses the 'Path.Rectangle' constructor, which takes a starting point and a size object. Stroke and fill colors can be customized. ```javascript var rect = new Path.Rectangle(new Point(20, 20), new Size(80, 40)); rect.strokeColor = 'red'; rect.fillColor = 'pink'; ``` -------------------------------- ### Create and Style Regular Polygons with Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/JSON/Random Path Testing.html This snippet demonstrates how to create regular polygons (like triangles and decahedrons) with specified center, number of sides, and radius using Paper.js. It also shows how to set fill color, clone paths, set stroke color, and rotate them. ```javascript var center = new Point(100, 100); var sides = 3; var radius = 50; var triangle = new Path.RegularPolygon(center, sides, radius); triangle.fillColor = 'black'; copy = triangle.clone(); copy.strokeColor = 'blue'; copy.rotate(45); var center = new Point(100, 300); var sides = 10; var radius = 50; var decahedron = new Path.RegularPolygon(center, sides, radius); decahedron.fillColor = 'black'; ``` -------------------------------- ### Drawing Arcs using Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/JSON/Raster.html Shows how to create an arc segment within a path using Paper.js. This involves defining the start point, end point, and control points for the arc. ```javascript var arc = new Path(); arc.strokeColor = 'purple'; arc.addSegment(new Point(100, 100), null, new Point(150, 50)); // Start point and first control point arc.addSegment(new Point(200, 100), new Point(150, 150), null); // Second control point and end point ``` -------------------------------- ### Create and Style PointText in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/SVG Export/Text Testing.html This snippet demonstrates how to create PointText objects, set their content, color, and apply transformations such as rotation and shearing. It requires the Paper.js library to be included in the project. ```javascript var text = new PointText(new Point(50, 100)); text.fillColor = 'black'; text.content = 'This is a test'; var text2 = new PointText(new Point(100, 150)); text2.fillColor = 'red'; text2.strokeWidth = '4'; text2.content = 'This is also a test'; /* text.scale(5); text.translate(15, 15); text.rotate(20); text.shear(20, 5); */ text.rotate(45); text.shear(.85, .15); text.scale(.85, 2); ``` -------------------------------- ### Drawing Arcs with Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Rasters/Raster.html Illustrates the creation of arc segments within a path using Paper.js. It shows how to add an arc by specifying the start point, end point, and control points. ```javascript var path = new Path(); path.add(new Point(100, 100)); path.arcTo(new Point(200, 100), true); path.strokeColor = 'orange'; ``` -------------------------------- ### Create and Style a Path in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/JSON/Raster.html This snippet demonstrates how to create a basic path with two points and apply a stroke color and width. It highlights fundamental path creation and styling in Paper.js. No external dependencies are required beyond the Paper.js library. ```javascript var path = new Path(); path.strokeColor = 'black'; path.strokeWidth = 10; path.add(new Point(100, 100)); path.add(new Point(200, 200)); ``` -------------------------------- ### Create and Render a Path in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Rasters/Raster.html Demonstrates how to create a simple path with segments and render it on the canvas using Paper.js. It requires the Paper.js library to be included in the HTML. ```javascript var path = new Path(); path.strokeColor = 'black'; path.add(new Point(100, 100)); path.add(new Point(200, 200)); path.add(new Point(100, 200)); path.add(new Point(200, 100)); ``` -------------------------------- ### Drawing an Arc in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Rasters/Raster.html Illustrates how to draw an arc segment for a path. This requires defining the arc's bounds, start and end angles. The output is a path segment forming an arc. ```javascript var arc = new Path.Arc({ from: new Point(100, 100), to: new Point(200, 100), clockwise: true, strokeColor: 'purple' }); ``` -------------------------------- ### JavaScript: Create and Initialize Path for Animation Source: https://github.com/paperjs/paper.js/blob/develop/examples/Paperjs.org/FutureSplash.html Creates a Paper.js 'Path' object and initializes its segments with 'Spring' connections. It sets up fixed points at the ends and adjusts the path's position. This function is called during initialization and resizing. Dependencies include Paper.js 'Path', 'Point', and the 'Spring' class. ```javascript function createPath(strength) { var path = new Path({ fillColor: 'black' }); springs = []; for (var i = 0; i <= values.amount; i++) { var segment = path.add(new Point(i / values.amount, 0.5) * size); var point = segment.point; if (i == 0 || i == values.amount) point.y += size.height; point.px = point.x; point.py = point.y; // The first two and last two points are fixed: point.fixed = i < 2 || i > values.amount - 2; if (i > 0) { var spring = new Spring(segment.previous.point, point, strength); springs.push(spring); } } path.position.x -= size.width / 4; return path; } ``` -------------------------------- ### Create Text with Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/JSON/Raster.html This example demonstrates how to create and style text objects using Paper.js. It shows how to specify the text content, font size, and color. ```javascript var text = new PointText({ point: new Point(100, 100), content: 'Hello, Paper.js!', fillColor: 'blue', fontSize: 16 }); ``` -------------------------------- ### Drawing a Line with Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Scripts/BlendModes.html This snippet illustrates how to draw a line segment between two points using Paper.js. It defines the start and end points of the line and sets its stroke color and width. ```javascript var startPoint = new Point(20, 20); var endPoint = new Point(150, 100); var line = new Path.Line(startPoint, endPoint); line.strokeColor = 'green'; line.strokeWidth = 2; ``` -------------------------------- ### Draw a Bezier Curve in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/SVG Export/Raster.html Shows how to draw a cubic Bezier curve using the Path.Bezier constructor. This requires specifying the start point, end point, and two control points. ```javascript var bezier = new Path.Bezier({ from: [100, 300], to: [300, 400], control1: [150, 250], control2: [250, 450] }); bezier.strokeColor = 'purple'; ``` -------------------------------- ### Basic Path Creation and Styling in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Scripts/BlendModes.html This snippet demonstrates the fundamental process of creating a custom path by defining its segments and then applying styling like stroke color and width. ```javascript var path = new Path(); path.add(new Point(50, 50)); path.add(new Point(100, 100)); path.add(new Point(50, 100)); path.strokeColor = 'black'; path.strokeWidth = 1; ``` -------------------------------- ### Create and Style a Circle in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Rasters/Raster.html Shows how to create a circle using the 'Path.Circle' constructor and apply fill and stroke colors. This snippet illustrates basic shape creation and styling. It requires a center point and radius. ```javascript var circle = new Path.Circle({ center: new Point(100, 100), radius: 50, fillColor: 'red', strokeColor: 'black' }); ``` -------------------------------- ### Handling Mouse Drag Events in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Paperjs.org/SpiralRaster.html This snippet shows how to handle mouse drag events in Paper.js. It draws a line segment from the starting point of the drag to the current mouse position. ```javascript var path; view.onMouseDown = function(event) { path = new Path(); path.strokeColor = 'blue'; path.add(event.point); } view.onMouseDrag = function(event) { path.add(event.point); } ``` -------------------------------- ### Creating Text with Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Rasters/Raster.html Shows how to create and style text objects in Paper.js. It involves using the PointText object and setting properties like content, font size, and color. ```javascript var text = new PointText(new Point(10, 30)); text.content = 'Hello, Paper.js!'; text.fillColor = 'purple'; text.fontSize = 16; ``` -------------------------------- ### Create an Arc Path in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Scripts/BlendModes.html This snippet shows how to create an arc path in Paper.js. It defines the bounding rectangle of the ellipse from which the arc is taken, and the start and end angles. The arc can be styled as needed. ```javascript var rect = new Rectangle(new Point(10, 10), new Size(100, 80)); var startAngle = 0; var endAngle = 180; var arcPath = new Path.Arc(rect, startAngle, endAngle); arcPath.strokeColor = 'orange'; arcPath.strokeWidth = 2; ``` -------------------------------- ### Initialize and Play Media Element Source: https://github.com/paperjs/paper.js/blob/develop/examples/Paperjs.org/NyanRainbow.html Initializes a MediaElement.js player for 'nyan.mp4', sets up a success callback to play the media and listen for time updates. It tracks if the video has played past 3.7 seconds. ```javascript var mediaElement; var playing = false; MediaElement('nyan', { pluginPath: '/assets/mediaelement/', success: function(me) { mediaElement = me; me.play(); me.addEventListener('timeupdate', function(time) { if (me.currentTime > 3.7) playing = true; }); } }); ``` -------------------------------- ### Drawing a Line Segment in Paper.js Source: https://github.com/paperjs/paper.js/blob/develop/examples/Scripts/BlendModes.html This snippet illustrates how to draw a line segment between two points using Paper.js. It defines the start and end points of the line and sets its stroke color and width. ```javascript var line = new Path.Line(new Point(20, 20), new Point(150, 100)); line.strokeColor = 'black'; line.strokeWidth = 2; ```