0.27.0 • Published 1 year ago

@dank-inc/sketchy v0.27.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Sketchy!

A super-dank sketching library built with ♥ and typescript. Inspired heavily by Canvas Sketch by @mattdesl - the difference is that this is meant to just drop into a webpage. Here is a live example

Getting Started

This library is a nice lightweight typescript wrapper for the 2d Web canvas. It's purpose is to give you a simple interface to the canvas, with minimal overhead.

The goal of this project is to give a creative coder an entrypoint between raw javascript and p5.js

start by creating a config object:

const params = createParams({
  containerId: 'root', // id of the container where you want a canvas
  animate: true, // animated?
  // dimensions: [1200, 1200], // size of canvas
})

then pass your sketch & config object into the sketch loader function:

import sketch from './sketches/sketch'

loadSketch(sketch, params)

Implementation Examples

React Wrapper Example here

Example running inside a react page here

Example Sketch

note: This library and it's helper functions all assume you are workng with normalized values :)

import { mapXY } from '@dank-inc/lewps'
import { createSketch, Vec2 } from '../lib'
import { hsl, hex } from '../lib/helpers'
import { createControls } from '../lib/helpers/controls'

// type the function, and all params are implicitly typed
export default createSketch((params) => {
  // destructure helper functions and convenience variables
  const { context, setFillStyle, setFilter, sin, cos, lerp } = params

  // initialize your sketch and objects
  const points = mapXY<Vec2>(15, 15, (u, v) => [u, v])

  const state = {
    x: 0,
    y: 0,
    lastKey: '',
    blur: false,
  }

  const [controls] = createControls({
    KeyQ: () => state.x--,
    KeyE: () => state.x++,
    Space: () => (state.blur = !state.blur),
  })

  return ({ width, height, t }) => {
    // draw loop function
    const lastKey = controls.shift()
    if (lastKey) state.lastKey = lastKey

    setFillStyle('#111')
    context.fillRect(0, 0, width, height)

    setFillStyle(hex(0.5, 0.5, 0.5))
    context.fillText(state.lastKey, 10, height - 100)

    for (let [u, v] of points) {
      const x = lerp(u, width, width / 3)
      const y = lerp(v, height, 200)

      setFillStyle(hsl(u, 0.5, 0.5))

      context.fillRect(
        x + state.x * 10,
        y + state.y * 10,
        cos(v, 1, 20),
        sin(t(0.3) + u, 1, 50),
      )
    }
  }
})

This will give you something that looks like this:

npm.io

0.27.0

1 year ago

0.26.3

2 years ago

0.26.2

2 years ago

0.26.1

2 years ago

0.26.0

2 years ago

0.25.0

2 years ago

0.21.0

2 years ago

0.24.0

2 years ago

0.23.0

2 years ago

0.22.0

2 years ago

0.21.1

2 years ago

0.20.0

3 years ago

0.19.1

3 years ago

0.19.0

3 years ago

0.17.2

3 years ago

0.17.3

3 years ago

0.15.0

3 years ago

0.16.0

3 years ago

0.17.0

3 years ago

0.16.1

3 years ago

0.18.0

3 years ago

0.17.1

3 years ago

0.14.1

3 years ago

0.14.0

3 years ago

0.13.1

3 years ago

0.13.2

3 years ago

0.13.4

3 years ago

0.13.0

3 years ago

0.12.2

3 years ago

0.11.0

3 years ago

0.12.0

3 years ago

0.8.0

3 years ago

0.7.0

3 years ago

0.6.0

3 years ago

0.5.0

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago