0.1.0 • Published 7 months ago

@forgen/canvas-drawing v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

canvas-drawing

A bunch of Javascript functions that make canvas drawing easier

  • 🚀 powered by Rust & WebAssembly, so the browser execute binary under the hood
  • 🌿 access with a fluent API, clean code first !
  • 🎨 Css-like properties names
  • 📖 and TS types included

Example with React

import { arrow, line, arrowHead, LineStyle, ellipse, diamond, text, LineCap } from 'canvas-drawing'
import React, { useEffect, useRef } from 'react'

function App() {
  const canvasRef = useRef<HTMLCanvasElement>(null)

  useEffect(() => {
    const context = canvasRef.current.getContext('2d')
    diamond()
      .x(140).y(0).width(60).height(40)
      .borderColor('yellow').backgroundColor('grey')
      .draw(context)

    ellipse().x(160).y(60).width(60).height(40)
      .borderColor('white')
      .borderStyle(LineStyle.Dashed)
      .draw(context)

    text("Hello").start(10, 90)
      .color('#FFF').fontSize(34)
      .draw(context)

    arrow()
      .body(
        line().from(10, 10).to(120, 100).width(6)
        .color('white').cap(LineCap.Round).quadraticCurve(160, 10)
      )
      .head(
        arrowHead().color('red').width(6).size(16)
      )
      .draw(context)

    postit(260, 10).draw(context)
    text("@TODO star repo")
      .start(270, 50)
      .fontFamily("Kalam")
      .bold().color("#333")
      .maxWidth(80)
      .draw(context)

  }, [])
  return (
    <canvas ref={canvasRef} />
  )

}

Screenshot

text

ParameterTypeDescription
startx: number, y: numberpostion of the start point
valuestringtext content
fontSizenumber
fontFamilystring
colorstringA string parsed as CSS color or a CanvasGradient object
borderColorstring
lineHeightnumberRatio based on font-size. Default: 1.2.
maxWidthnumberIf given, text will be cropped and multi-lined automatically.
fontWeightFontWeightNormal, Light, Bold
boldShortup for setting lineWeight to Bold
fontStyleFontStyleNormal, Italic, Oblique. Italic vs Oblic : Italic is described in the font declaration. Oblique is a browser text slant.
italicShortup for setting lineStyle to Italic
underlineboolundefinedno args (undefined) is a shortcup for true
strikethroughboolundefinedno args (undefined) is a shortcup for true
    text("This is an auto cropped text based on the maxWidth property")
      .start(0, 40)
      .color('#FFF')
      .fontSize(18)
      .maxWidth(200)
      .fontFamily("Arial")
      .draw(context)
    line().from(200,0).to(200,100).width(2).color('red').draw(context)

Screenshot

line

ParameterTypeDescription
fromx: number, y: numberpostion of the start point
tox: number, y: numberposition of the end point
widthnumberwidth of the line
styleLineStyleDefault: LineStyle.SOLID
capLineCapDefault: LineCap.BUTT
colorstringA string parsed as CSS color or a CanvasGradient object
quadraticCurvex: number, y: numberThe quadratic curve control point
bezierCurvex1: number, y1: number, x2: number, y2: numberThe bezier curve control points

arrow

ParameterType
bodyLine
headArrowHead

arrowHead

ParameterTypeDescription
widthnumberwidth of the line
sizenumberarrow head size
colorstringA string parsed as CSS color or a CanvasGradient object

rectangle, ellipse, diamond

ParameterTypeDescription
xnumberX position of the top left corner
ynumberY position of the top left corner
widthnumberTotal width of the rectangle, borders included
heightnumberTotal height of the rectangle, borders included
opacitynumberBetween 0 and 1. Default: 1
backgroundColorstringA string parsed as CSS color or a CanvasGradient object
borderStyleLineStyleDefault: LineStyle.SOLID
borderColorstringA string parsed as CSS color or a CanvasGradient object
borderWidthnumberDefault: 4

postit

ParameterTypeDescription
widthnumberwidth of the postit (shadow excluded)
heightnumberheight of the postit (shadow excluded)
xnumberx coordinate
ynumbery coordinate
anglenumberrotate angle in degrees (default : 3)
colorstringA string parsed as CSS color or a CanvasGradient object
0.1.0

7 months ago