- var canvas;
- var ctx;
- window.onload = function(){
- canvas = document.getElementById("canvas")
- ctx = canvas.getContext('2d')
- }
- function Circle(ctx,x,y,r){
- // x = top corner X y = top corner Y
- // r = radius ctx = canvas context
- ctx.fillStyle = "#000000"
- ctx.beginPath();
- ctx.arc(x+r,y+r,r,0,Math.PI*2,true);
- ctx.stroke();
- }
- function rectRound(ctx,x,y,width,height,radius){
- ctx.beginPath();
- ctx.moveTo(x,y+radius);
- ctx.lineTo(x,y+height-radius);
- ctx.quadraticCurveTo(x,y+height,x+radius,y+height);
- ctx.lineTo(x+width-radius,y+height);
- ctx.quadraticCurveTo(x+width,y+height,x+width,y+height-radius);
- ctx.lineTo(x+width,y+radius);
- ctx.quadraticCurveTo(x+width,y,x+width-radius,y);
- ctx.lineTo(x+radius,y);
- ctx.quadraticCurveTo(x,y,x,y+radius);
- ctx.stroke();
- ctx.fill();
- }
- function prism(ctx,x,y,width,height,depth,lineWidth,fillStyle){
- // x = top corner X y = top corner Y
- // ctx = canvas context
- // example: prism(ctx,150,150,200,200,200,2,"rgba(22,0,22,.9)")
- depth /= 3;
- // COLORS
- ctx.strokeStyle = "rgba(0,0,0,0)";
- ctx.fillStyle = fillStyle;
- ctx.beginPath();
- ctx.moveTo(x,y);
- ctx.lineTo(x,y+width);
- ctx.lineTo(x+width,y+width);
- ctx.lineTo(x+width+depth,y+width-depth)
- ctx.lineTo(x+width+depth,y-depth)
- ctx.lineTo(x+depth,y-depth)
- ctx.fill();
- // LINES
- ctx.strokeStyle = "#000000"
- ctx.lineWidth = lineWidth;
- ctx.lineJoin = "miter";
- ctx.beginPath();
- ctx.moveTo(x,y);
- ctx.lineTo(x+width,y);
- ctx.lineTo(x+width,y+height);
- ctx.lineTo(x,y+height);
- ctx.lineTo(x,y);
- ctx.lineTo(x+width,y);
- // MOVING X AND Y DEFAULTLY
- x+=width;y+=height;
- ctx.moveTo(x,y);
- ctx.lineTo(x+depth,y-depth);
- ctx.lineTo(x+depth,y-height-depth);
- ctx.lineTo(x,y-height)
- ctx.moveTo(x+depth,y-height-depth);
- ctx.lineTo(x+depth-width,y-height-depth);
- ctx.lineTo(x-width,y-height);
- ctx.stroke();
- }
- function cube(ctx,x,y,width,lineWidth,fillStyle){
- // x = top corner X y = top corner Y
- // ctx = canvas context
- // example: cube(ctx,150,150,20,2,"rgba(44,22,44,1)")
- depth = width / 3;
- height = width;
- // COLORS
- ctx.strokeStyle = "rgba(0,0,0,0)";
- ctx.fillStyle = fillStyle;
- ctx.beginPath();
- ctx.moveTo(x,y);
- ctx.lineTo(x,y+width);
- ctx.lineTo(x+width,y+width);
- ctx.lineTo(x+width+depth,y+width-depth)
- ctx.lineTo(x+width+depth,y-depth)
- ctx.lineTo(x+depth,y-depth)
- ctx.fill();
- // LINES
- ctx.strokeStyle = "#000000"
- ctx.lineWidth = lineWidth;
- ctx.lineJoin = "miter";
- ctx.beginPath();
- ctx.moveTo(x,y);
- ctx.lineTo(x+width,y);
- ctx.lineTo(x+width,y+height);
- ctx.lineTo(x,y+height);
- ctx.lineTo(x,y);
- ctx.lineTo(x+width,y);
- // MOVING X AND Y DEFAULTLY
- x+=width;y+=height;
- ctx.moveTo(x,y);
- ctx.lineTo(x+depth,y-depth);
- ctx.lineTo(x+depth,y-height-depth);
- ctx.lineTo(x,y-height)
- ctx.moveTo(x+depth,y-height-depth);
- ctx.lineTo(x+depth-width,y-height-depth);
- ctx.lineTo(x-width,y-height);
- ctx.stroke();
- }
- function transPrism(ctx,x,y,width,height,depth,lineWidth,opacity,fillStyle){
- // x = top corner X y = top corner Y
- // ctx = canvas context
- // example: transPrism(ctx,150,150,200,200,200,2,.5)
- prism(ctx,x,y,width,height,depth,lineWidth,fillStyle);
- ctx.lineWidth = lineWidth;
- ctx.lineJoin = "miter";
- ctx.strokeStyle = "rgba(0,0,0," + opacity + ")"
- depth /= 3;
- ctx.moveTo(x+depth,y+height-depth);
- ctx.lineTo(x+depth,y-depth);
- ctx.moveTo(x+depth,y+height-depth);
- ctx.lineTo(x,y+height);
- ctx.moveTo(x+depth,y+height-depth);
- ctx.lineTo(x+width+depth,y+height-depth);
- ctx.stroke();
- }
Sunday, August 17, 2014
JavaScript Routines Part 2: Canvas (whole)
So what this is here is some things I've pieced together, including drawing prisms and cubes.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment