Demo entry 6698596

javascript

   

Submitted by anonymous on Jan 25, 2018 at 14:38
Language: JavaScript. Code size: 2.9 kB.

function JSQCanvasPainter(canvas) {
	var that=this;
	var ctx=canvas[0].getContext('2d');

	var m_pen={color:'black'};
	var m_font={"pixel-size":12,family:'Arial'};
	var m_brush={color:'black'};

	this.pen=function() {return JSQ.clone(m_pen);};
	this.setPen=function(pen) {setPen(pen);};
	this.font=function() {return JSQ.clone(m_font);};
	this.setFont=function(font) {setFont(font);};
	this.brush=function() {return JSQ.clone(m_brush);};
	this.setBrush=function(brush) {setBrush(brush);};

	this._initialize=function(W,H) {
		//ctx.fillStyle='black';
		//ctx.fillRect(0,0,W,H);
	};
	this._finalize=function() {
	};
	this.clearRect=function(x,y,W,H) {
		ctx.clearRect(x,y,W,H);
	};
	this.fillRect=function(x,y,W,H,brush) {
		if (typeof brush === 'string') brush={color:brush};
		if (!('color' in brush)) brush={color:brush};
		ctx.fillStyle=to_color(brush.color);
		ctx.fillRect(x,y,W,H);
	};
	this.drawRect=function(x,y,W,H) {
		ctx.strokeStyle=to_color(m_pen.color);
		ctx.strokeRect(x,y,W,H);
	};
	this.drawPath=function(painter_path) {
		ctx.strokeStyle=to_color(m_pen.color);
		painter_path._draw(ctx);
	};
	this.drawLine=function(x1,y1,x2,y2) {
		var ppath=new JSQPainterPath();
		ppath.moveTo(x1,y1);
		ppath.lineTo(x2,y2);
		that.drawPath(ppath);
	};
	this.drawText=function(rect,alignment,txt) {
		var x,y,textAlign,textBaseline;
		if (alignment.AlignLeft) {
			x=rect[0];
			textAlign='left';
		}
		else if (alignment.AlignCenter) {
			x=rect[0]+rect[2]/2;
			textAlign='center';
		}
		else if (alignment.AlignRight) {
			x=rect[0]+rect[2];
			textAlign='right';
		}

		if (alignment.AlignTop) {
			y=rect[1];
			textBaseline='top';
		}
		else if (alignment.AlignBottom) {
			y=rect[1]+rect[3];
			textBaseline='bottom';
		}
		else if (alignment.AlignVCenter) {
			y=rect[1]+rect[3]/2;
			textBaseline='middle';
		}

		ctx.font=m_font['pixel-size']+'px'+' '+m_font.family;
		ctx.textAlign=textAlign;
		ctx.textBaseline=textBaseline;
		ctx.strokeStyle=to_color(m_pen.color);
		ctx.fillStyle=to_color(m_brush.color);
		ctx.fillText(txt,x,y);
	}
	this.drawEllipse=function(rect) {
		ctx.strokeStyle=to_color(m_pen.color);
		ctx.beginPath();
		ctx.ellipse(rect[0]+rect[2]/2,rect[1]+rect[3]/2,rect[2]/2,rect[3]/2,0,0,2*Math.PI);
		ctx.stroke();
	}
	this.fillEllipse=function(rect) {
		ctx.fillStyle=to_color(m_brush.color);
		ctx.beginPath();
		ctx.ellipse(rect[0]+rect[2]/2,rect[1]+rect[3]/2,rect[2]/2,rect[3]/2,0,0,2*Math.PI);
		ctx.fill();
	}

	function setPen(pen) {
		m_pen=JSQ.clone(pen);
	}

	function setFont(font) {
		m_font=JSQ.clone(font);
	}

	function setBrush(brush) {
		m_brush=JSQ.clone(brush);
	}

	function to_color(col) {
		if (typeof col === 'string') return col;
		return 'rgb('+Math.floor(col[0])+','+Math.floor(col[1])+','+Math.floor(col[2])+')';
	}
}

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).