Giter Site home page Giter Site logo

rafaelcamaram / paint.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from squarific/paint.js

0.0 2.0 0.0 902 KB

Paint.js is a javascript library to create apps focused on simultanious drawing.

License: MIT License

JavaScript 88.25% CSS 11.45% HTML 0.30%

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

Watchers

James Cloos avatar Rafael Câmara avatar

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.