? Browser Support ?
HTML
>
Canvas
>
Processing
>Multi
Kommuniksajon mellom skisser
De to canvas-elementene har identisk processing kode.
_multiDraw.pde
/* Drawing simple lines with width and color lines are arranged in strokes */ // thin and black int strokeWidth=2; color strokeColor=color(255, 204, 0); // recorded pontvalues int lastX=0; int lastY=0; // idetifier so it can present itself int myIdentifier=-1; // all strokes ArrayList<pstroke> strokes; // and the one we are working on pstroke currentStroke=null; void setup() { size(300, 200); smooth(); frameRate(60); strokes=new ArrayList<pstroke>(); currentStroke=null; clearCanvas(); } void clearCanvas() { background(255, 255, 180); } //------------------------------------- // mousing only active when canDraw == true // pressed mouse and we start a new stroke void mousePressed() { addStroke(strokeWidth, strokeColor); currentStroke.addpt(new pt(mouseX, mouseY)); } // dragged and we add a point void mouseDragged() { if (currentStroke!=null){ if((Math.abs(lastX-mouseX)>3) || (Math.abs(lastY-mouseY)>3)){ currentStroke.addpt(new pt(mouseX, mouseY)); lastX=mouseX; lastY=mouseY; } } } void draw() { clearCanvas(); for (int ix=0;ix < strokes.size();ix++) ((pstroke)strokes.get(ix)).drawStroke(); } // add a new stroke void addStroke(int strokeWidth, int strokeColor) { currentStroke=new pstroke(strokeWidth, strokeColor); strokes.add(currentStroke); } // add a new point to current stroke void addPoint(int x, int y) { currentStroke.addpt(new pt(x, y)); } //------------------------ void clearLast(){ if(strokes.size() >0) strokes.remove(strokes.size()-1); } // this is the finnish of a stroke // tell ths surrounding about this event void mouseReleased(){ shareStroke(myIdentifier,currentStroke); } void setIdentifier(int id){ myIdentifier=id; } void setInitColor(int r,int g, int b){ strokeColor=color(r, g, b); } int getIdentifier(){ return myIdentifier; } void addStrokeFromPage(int strokeWidth, int strokeColor){ currentStroke=new pstroke(strokeWidth, strokeColor); strokes.add(currentStroke); }
_pstroke.pde
// hold a stroke with: // color, width, type, timeused and a series of points class pstroke { private int pwidth; private color pcolor; // all the points in the stroke private ArrayList<pt> pts; // constructor public pstroke(int pwidth, color pcolor) { this.pwidth=pwidth; this.pcolor=pcolor; this.pts=new ArrayList<pt>(); } // add a point (no difflimit) public void addpt(pt p) { pts.add(p); } // draw this stroke with all lines public void drawStroke() { // set color and width strokeWeight(this.pwidth); stroke(this.pcolor); // loop points int ix=1; while (ix < pts.size ()) { line( pts.get(ix-1).x, pts.get(ix-1).y, pts.get(ix).x, pts.get(ix).y); ix++; } } }
_pt.pde
// hold a single point class pt { public int x, y; // constructor public pt(int x, int y) { this.x=x; this.y=y; } public String toString() { return ""+x+","+y; } }
Javascriptet som setter opp de to skissene og handterer datautvekslingen mellom dem slik ut:
_index.js
var CanvasList=new Array(); // clear all function clearDrawing(id){ var pjs = Processing.getInstanceById(id); pjs.setup(); } // clear last stroke function clearLastStroke(id){ var pjs = Processing.getInstanceById(id); pjs.clearLast(); } // called from sketch for sharing a stroke to other sketch function shareStroke(senderId,strokeObj){ for( var ix=0;ix < CanvasList.length;ix++){ var pjs = CanvasList[ix]; var currentId=pjs.getIdentifier(); if( currentId!=senderId){ // we cannot copy the stroke blindly. We must let // javascript identify the components and // simulate a drawing action by adding points pjs.addStrokeFromPage(strokeObj.pwidth,strokeObj.pcolor); // mirror each point when adding points to the new stroke for(var pix=0; pix < strokeObj.pts.size();pix++){ pjs.addPoint(300-strokeObj.pts.get(pix).x, strokeObj.pts.get(pix).y); } } } } // set up sketches function identifyPObj(){ // show them document.getElementById("wrapper1").style.display="block"; document.getElementById("wrapper2").style.display="block"; // hide the startup button document.getElementById("starter").style.display="none"; // give each processing instance an id so it can identify // itself when it reports, and give it a basic drawing color var pjs = Processing.getInstanceById('draw0'); pjs.setIdentifier(0); pjs.setInitColor(0,0,0); CanvasList[0]=pjs; pjs = Processing.getInstanceById('draw1'); pjs.setIdentifier(1); pjs.setInitColor(255,0,0); CanvasList[1]=pjs; }