Tegning med Processing.js
Her tegningen lagt i en iframe.
Du kan se på siden og inspisere all koden i sketch https://borres.hiof.no/wep/proc/procjs/sketching/sketch.html
Det er to processing-filer involvert:
_sketching.pde
/* Drawing simple lines with width and color lines are ordered in strokes. Use PVector for point. */ // initial values, thin and black int strokeWidth=1; int strokeColor=unhex("FF000000"); int backColor= unhex("FFFFFFFF"); // flag for allowing draw or not boolean canDraw=true; // all strokes ArrayList<pstroke> strokes; // and the one we are working on pstroke currentStroke=null; void setup() { size(400, 400); smooth(); frameRate(20); strokes=new ArrayList<pstroke>(); currentStroke=null; } void draw() { background(255, 255, 180); background(backColor); for(pstroke s:strokes) s.drawStroke(); } //------------------------------------- // mousing only active when canDraw == true // pressed mouse and we start a new stroke void mousePressed() { addStroke(strokeWidth, strokeColor); currentStroke.addpt(new PVector(mouseX, mouseY)); } void mouseDragged() { if (currentStroke!=null) currentStroke.addpt(new PVector(mouseX, mouseY)); } void mouseReleased() { if (currentStroke!=null) { currentStroke.addpt(new PVector(mouseX, mouseY)); } } //---------------------------------- // add a new stroke void addStroke(int strokeWidth, int strokeColor) { currentStroke=new pstroke(strokeWidth, strokeColor); strokes.add(currentStroke); } // remove latest stroke // called from javascript void removeStroke() { strokes.remove(strokes.size()-1); currentStroke=null; } // allow mousing or not // set from javascript void setDrawable(boolean b) { canDraw=b; } // set weight // set from javascript void setStrokeWidth(int w) { strokeWidth=w; } // set stroke color // set from javascript void setStrokeColor(String c) { strokeColor=unhex(c); } // set backcolor // set from javascript void setBackColor(String c) { backColor=unhex(c); }
_pstroke.pde
// hold a stroke with: // color, width and a series of points (Pvector) class pstroke { private int pcolor, pwidth; // all the points in the stroke private ArrayList<PVector> pts; // constructor public pstroke(int pwidth, int pcolor) { this.pwidth=pwidth; this.pcolor=pcolor; this.pts=new ArrayList<PVector>(); } // add a point public void addpt(PVector 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++; } } }
Javaskriptet som kommuniserer med Processing er slik:
_index.js
// init, set penwidth, stroke color and background var pSketch; function init(){ pSketch=Processing.getInstanceById('drawFace'); setPenColor(); setCanvasColor() setPenWidth(); } //--------------------------------- // react to users choice from menu // clear everything function clearData(targetNodeId,procId){ var pjs = Processing.getInstanceById(procId); pSketch.setup(); pSketch.draw(); } // clear last stroke function removeStroke(procId){ pSketch.removeStroke(); } // pen width function setPenWidth(){ var w=document.forms['colorform']['penwidth'].value; pSketch.setStrokeWidth(w); } // pen color function setPenColor(){ var c=document.forms['colorform']['color1'].value; c='FF'+c.substr(1).toUpperCase(); pSketch.setStrokeColor(c); } // canvascolor function setCanvasColor(){ var c=document.forms['colorform']['color2'].value; c='FF'+c.substr(1).toUpperCase(); pSketch.setBackColor(c); }