GithubHelp home page GithubHelp logo

gambar's Introduction

gambar

GitHub top language GitHub license GitHub issues

Gambar is a small drawing library written in Typescript. The library provides a thin, consistent, layer on top of some basic Canvas API methods. Gambar doesn't aim to be comprehensive, but rather, to provide convenience when implementing a drawing app with limited scope.

The name is Indonesian and translates to picture, drawing, or to draw.

screenshot

API

Getting Started

To start, create a new instance of Gambar and pass in your canvas element. Gambar's constructor also accepts an optional parameter, boundingBoxStyle, in case the library is used in an interactive drawing app.

const canvas = document.querySelector("canvas")
const gambar = new Gambar(canvas)

// Also possible
const boundingBoxStyle = {
    bBoxNodeStyle,
    bBoxEdgeStyle,
}
const gambar = new Gambar(canvas, boundingBoxStyle)

Creating Shapes

rectangle(pt0: Point, pt1: Point, style: StyleProps, save = true)

ellipse(pt0: Point, pt1: Point, style: StyleProps, save = true)

line(pt0: Point, pt1: Point, style: StyleProps, save = true)

Most shapes have the same signature: shape(startPt, endPt, style).

startPt and endPt are Point objects with {x, y} members.

style is a StyleProps object with {strokeColor, strokeWidth, fillColor} optional members.

When save is false, the shape is only drawn temporarily and won't be pushed onto the shapes stack.

gambar.rectangle(
  {x: 100, y: 100},
  {x: 200, y: 150},
  {strokeColor: "red", strokeWidth: 2, fillColor: "white"})

gambar.ellipse(startPt, endPt, style)
gambar.line(startPt, endPt, style)

polygon(points: Point[], style: StyleProps, save = true)

The polygon method creates an arbitrary polygon and takes an array of Point objects and a style object.

gambar.polygon(
  [
    {x: 10, y: 20},
    {x: 100, y: 200},
    {x: 50, y: 100}
  ],
  {strokeColor: "blue", strokeWidth: 3}
)

Other Methods

render()

Forces the entire canvas to be re-rendered. This method is called by methods that affect the canvas (eg. drawing methods).

deleteAll()

Delete all shapes in memory.

deleteShape(shape: Shape)

Delete specified shape.

clearSelection()

Deselects all shapes.

selectShapeAtPoint(point: Point, clearSelection = true)

Select the top-most shape that intersects the point (ie. pointer location).

findShapeAtPoint(point: Point)

Find the top-most shape that intersects the point. This method doesn't actually select the shape, but returns it.

pushSelectedShapesBackward()

Push the selected shapes further down the layer stack.

pullSelectedShapesForward()

Pull the selected shapes up the layer stack.

moveSelectedShapes(delta: Point)

Move selected shapes by delta. For example, if shape's origin is at (10, 20) and delta is (100, 50), then the shape's new origin will be (110, 70).

loadStack(shapes: Shape[])

Load the drawing shapes stack.

getShapeStack()

Get the drawing shapes stack

getDrawingData()

Return the canvas' dataURL

gambar's People

Contributors

dependabot[bot] avatar jtanadi avatar

Stargazers

 avatar

Watchers

 avatar

gambar's Issues

Consider immutability

  • Should drawing.shapes be immutable?
  • Should each object be immutable and return a new instance when a property is edited?
  • Would it work like this?
// in Shape
updateFill(newFill) {
  const updated = _.deepClone(this)
  updated.fillColor = newFill
  return updated
}

// in Gambar
editSelected(callback) {
  this.shapes = this.shapes.map(shape => {
    if (shape.selected) {
      callback(shape)
    }
  })
}

// client code
gambar.editSelected(selectedShape => selectedShape.updateFill("#000"))

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.