Giter VIP home page Giter VIP logo

paint.js's Introduction

Paint.js

A drawing app that can easily be used to create networked drawing apps.

LICENSE: MIT

Demo

Pure paint.js:

http://squarific.github.io/Paint.js/demo.html

Used in another project:

http://www.anondraw.com

Dependency

jQuery is required

How to use

Include Paint.min.js, paint.css and spectrum.css Make sure the container has the position property set!

Create a paint object:

var container = document.getElementById("container");
var paint = new Paint(container);

Now you can bind to events:

paint.addEventListener("drawing", function (event) {
	console.log(event.type);
	console.log(event.drawing);
});

The above code will output the following when a line is drawn:

"drawing"
{type: "line", x: 4, y: 5, x1: 6, y1: 7, size: 10, color: "#ffaabb"}

Methods

paint.drawDrawings(layer, drawingArray);
paint.drawDrawing(layer, drawing);

These functions will put the drawings on the given layer. Layer can be 'public' or 'local'.

paint.changeTool(tool);
paint.changeColor(color);
paint.changeToolSize(size);

These functions allow you to change the tool, color and size. Tool can be one of the following: "grab", "line", "brush" or "block"*

*Block not yet implemented

paint.clear();

Clears all drawings.

paint.newCanvasOnTop(name)

Returns a canvas that will be automatically resized, it's .leftTopX and .leftTopY constantly updated to reflect it's 'world' coordinates and is added on top of the other canvasses except the effect canvas. It will have a class of className = "paint-canvas paint-canvas-" + name;

Events

{
	type: "userdrawing"             // The type of event
	drawing: {type: "brush", ...}   // The drawing that was just added
	removeDrawing: function () {}   // Remove the drawing from the local layer
	                                // For example when the server acknowledged the drawing
	                                // or if the user wasn't allowed to draw
}

Drawing types

Brush (dot):

{type: "brush", x: int, y: int, size: int, color: string}

Block:

{type: "block", x: int, y: int, size: int, color: string}

Line:

{type: "line", x: int, y: int, x1: int, y1: int, size: int, color: string}

Controls

Controls can be added to

paint.controlContainer; // Dom element that contains all controllers

Adding new tools

If you want to add a new tool you have to do 2 things. Add a button and add a event handling function.

Adding the button

The button will look like this:

{
    name: "toolName",
    type: "button",
    image: "images/icons/toolName.png",
    title: "Change tool to toolName",
    value: "toolNameFunction",
    action: this.changeTool.bind(this)
}

This object should be added to the createControlArray function. You should also add your tools name to the toolButtons array. ToolName and toolNameFunction should be the same because of the selection state of the buttons.

Adding the event handler

The event handler looks like this:

function toolNameFunction (paint, event) {

}

This function should be added to 'Paint.tools'.

Possible events are:

"remove"

{type: "mousedown", ...}
{type: "mousemove", ...}
{type: "mouseup", ...}

{type: "touchstart", ...}
{type: "touchmove", ...}
{type: "touchend", ...}

You can then use all methods on the paint object, some you will need are:

// Returns the coordinates of the event relative to the canvas
// To get relative to the world, do + paint.layer.leftTopX and leftTopY
paint.getCoords(event);

// TiledCanvas objects for the last layer and the local layer
paint.public
paint.local

// The canavas and context on top of all other layers
paint.effectCanvas
paint.effectCanvasCtx

Template:

if (event == "remove") {
	delete paint.lastPickerPoint;
	return;
}

// Get the coordinates relative to the world
var targetCoords = paint.getCoords(event);

if ((event.type == "mousedown" || event.type == "touchstart") && !paint.lastPickerPoint) {
	
}

if (event.type == "mouseup" || event.type == "touchend") {
	
}

if (event.type == "mousemove" || event.type == "touchmove") {
	
}

Adding new drawing types

TBA

paint.js's People

Contributors

intorfloat avatar annabelbeta avatar ketanhwr avatar lukasdietrich avatar

Stargazers

 avatar Veselin Mitev avatar  avatar  avatar DingDang avatar hoangitk avatar Ildar avatar S.H. avatar  avatar Mr.HyperBit avatar Torrentero avatar  avatar Intellectus-Sub avatar Luis Geniole avatar  avatar Leon S Kenedy avatar  avatar TAKUTO YOSHIKAI avatar  avatar Ermine avatar  avatar Marcell Lanczos avatar programking avatar Grace Kim Howard avatar Egor Zaykov avatar Todor Imreorov avatar David Welling avatar Luis Bustamante Flores avatar juandav avatar mathbbN avatar Lei Yang avatar  avatar J.C. avatar Harry avatar Efrem Ropelato avatar felinx avatar Buckaroo Cheung avatar  avatar Avery Smets avatar  avatar  avatar Zoe Stenger avatar Michael Anthony avatar  avatar  avatar Luke Shimkus avatar

Watchers

James Cloos avatar Michael Anthony avatar Avery Smets avatar Rich Henderson avatar Heraclio Rios avatar  avatar  avatar  avatar

paint.js's Issues

Stamps

Add little stamps:

  • A green ok
  • A thumbs up
  • A thumbs down
  • A smiley

Key bindings

LALT, while in brush to get color picker
LSHIFT, straight lines

1-9 Opacity

Default colors

Add some default colors to the toolbar for quick color change

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.