npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@urpflanze/drawer-canvas

v0.3.4

Published

Draw Urpflanze scene in browser or Node with canvas

Downloads

6

Readme

Synopsis

This package is used to draw a scene created with the Urpflanze Core on Canvas.

You can export a single frame or an animation in ZIP (frames in png or jpg), video (mp4 or webp) or in GIF.

You can use it in the browser (Canvas, OffscreenCanvas, ServiceWorker) or in Node.

Donate

I am trying to create a tool for those who want to approach the world of programming or for programmers who want to approach the world of creative coding.

I have spent a lot of time and will spend more to support this project. I also have in mind a web editor (open-source) where you can use the features of this library in the browser.

You can see a preview here


Menu

Installation

You can install the library with the command:

npm i @urpflanze/drawer-canvas --save

And import into your project

/**
 * Full importing
 */
import { BrowserDrawerCanvas, DrawerCanvas } from '@urpflanze/drawer-canvas'

const scene = ... // Urpflanze Scene

const drawer = new BrowserDrawerCanvas(scene, ...) // for browser
// const drawer = new DrawerCanvas(scene, ...) for node

Otherwise you can use from the browser using a CDN

<!-- ES Modules -->
<script type="module">
	import * as Urpflanze from 'https://esm.run/@urpflanze/core'
	import DrawerCanvas, { ... } from 'https://esm.run/@urpflanze/drawer-canvas'

	const scene = new Urpflanze.Scene()
	const drawer = new DrawerCanvas(scene, document.body) // as BrowserDrawerCanvas
</script>

<!-- UMD -->
<script src="https://cdn.jsdelivr.net/npm/@urpflanze/core"></script>
<script src="https://cdn.jsdelivr.net/npm/@urpflanze/drawer-canvas"></script>
<script>
	const scene = new Urpflanze.Scene()
	const drawer = new DrawerCanvas.default(scene, ...) // or DrawerCanvas.BrowserDrawerCanvas
</script>

BrowserDrawerCanvas

You can render the scene on canvas using the draw method


// Creating a Scene
const scene = new Urpflanze.Scene()
scene.add(...)

// Draw the scene
const drawer = new DrawerCanvas(scene, document.body) // The canvas will be added to body
drawer.draw()

Timeline and Animation

You can set animation duration and FPS using the timeline object

const drawer = new DrawerCanvas(scene, document.body)

drawer.timeline.setDuration([number in milliseconds])
drawer.timeline.setFramerate([number])

// draw at time
drawer.timeline.setTime([number in milliseconds])

By default the duration of an animation is 1 minute (60000 milliseconds) the framerate is 30 instead.

To start the animation you can use the startAnimation method:

const drawer = new DrawerCanvas(scene, document.body)
drawer.startAnimation()

// drawer.pauseAnimation(): stop the animation
// drawer.playAnimation(): start animation if is stopped or paused
// drawer.stopAnimation(): stop the animation and return to the start

Renderer

You can export a frame using the frame or frameAtTime methods, export a ZIP (using JSZip) containing the frames (jpeg or png), export a video (using FFMPEG) (mp4, webp or gif).

Video

Example of export video in a browser:

const scene = new Urpflanze.Scene()

scene.add(
	new Urpflanze.Rect({
		// rotate from 0 to 45 deg in 3000ms
		rotateZ: () => -(Math.cos((scene.currentTime * Urpflanze.PI2) / 6000) * 0.5 + 0.5) * (Math.PI / 2),
	})
)

const drawer = new DrawerCanvas(scene, document.body, {}, 3000 /* duration */, 24 /* fps */, 'linear' /* tick mode */)

const renderer = new Renderer(drawer)
renderer.render('video/mp4', 1).then(buffer => {
	const blob = new Blob([buffer], { type: 'video/mp4' })
	const videoUrl = window.URL.createObjectURL(blob)
	const videoElement = document.createElement('video')
	videoElement.setAttribute('src', videoUrl)
	videoElement.setAttribute('loop', 'true')
	videoElement.setAttribute('controls', 'true')
	document.body.appendChild(videoElement)
})

Example of export video in node:

const fs = require('fs')

const scene = new Urpflanze.Scene()

scene.add(
	new Urpflanze.Rect({
		// rotate from 0 to 45 deg in 3000ms
		rotateZ: () => -(Math.cos((scene.currentTime * Urpflanze.PI2) / 6000) * 0.5 + 0.5) * (Math.PI / 2),
	})
)

const drawer = new DrawerCanvas(scene, undefined, {}, 3000 /* duration */, 24 /* fps */, 'async' /* tick mode */)

const renderer = new Renderer(drawer /*, ffmpegCorePath*/)
renderer.render('video/mp4', 1).then(buffer => {
	fs.writeFileSync('video.mp4', buffer)
	process.exit()
})

You need to run with this flags for use FFMPEG

node --experimental-wasm-threads --experimental-wasm-bulk-memory [name].js

ZIP

Example of export ZIP in a browser:

// After creating a scene
const drawer = new DrawerCanvas(scene, document.body, {}, 3000, 10)
const renderer = new Renderer(drawer)
renderer.zip('image/png' /*, quality, framesForChunk */).then(chunks => {
	chunks.forEach((chunk, index) => {
		const blob = new Blob([chunk], { type: 'application/zip' })
		const chunkURL = window.URL.createObjectURL(blob)
		const a = document.createElement('a')
		a.innerText = 'download_' + index + '.zip'
		a.setAttribute('href', chunkURL)
		document.body.appendChild(a)
	})
})

Renderer events

You can attach functions to events called when rendering with the 'attach' method:

const renderer = new Renderer(drawer)

renderer.attach('[eventName]', eventArgs => console.log(eventArgs))

| Event Name | Description | | ------------------------ | ------------------------------------------------------------- | | renderer:zip_start | Called when start ZIP rendering | | renderer:zip_progress | Called each frame render | | renderer:zip_preparing | Called when each frame is rendered and ZIP generation start | | renderer:video_init | Called when start video rendering before loading FFmpeg.wasm | | renderer:video_start | Called when FFmpeg.wasm is loaded | | renderer:video_progress | Called each frame render | | renderer:video_preparing | Called when each frame is rendered and video generation start |

Quando renderizzi un video puoi intercettare i log di FFmpeg passando gli argomenti al metodo render

const renderer = new Renderer(drawer)

renderer.render(
	'gif',
	1,
	e => console.log('ffmpeg log', e),
	e => console.log('ffmpeg progress', e)
)

DrawerOptions

Any parameter is optional

| Param | Type | Default | Description | | ------------------ | ------------------------ | ------------ | ----------------------------------------------------------------------- | | time | number | 0 | Draw at time | | noBackground | boolean | false | Disable Scene background | | ghosts | number | 0 | Number of previus frame based on ghostSkipTime or ghostSkipFunction | | ghostAlpha | boolean | true | Enable ghost apha | | ghostSkipTime | number | 30 | Delay of each frame | | ghostSkipFunction | Function | undefined | Dynamic ghostSkipTime | | width | number | scene width | The canvas width | | height | number | scene height | The canvas height | | clear | boolean | true | Clear canvas | | simmetricLines | number | undefined | Utility lines | | sceneFit | cover | contain | none | contain | Fit scene into canvas | | backgroundImage | CanvasImageSource | undefined | Draw image after scene background | | backgroundImageFit | cover | contain | none | cover | backgroundImage fit | | loop | boolean | true | Repeat the animation in loop |