Created
March 19, 2012 12:59
-
-
Save rvaronos/2111171 to your computer and use it in GitHub Desktop.
Drawing curves.. demo and reference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/********************************************************************* | |
* Licensed under the Open Software License version 3.0 * | |
* * | |
* This Open Software License (OSL-3.0) applies to any original work * | |
* of authorship "vfold" whose owner Raphael Varonos has placed the * | |
* following licensing notice adjacent to the copyright notice for * | |
* the Original Work * | |
*********************************************************************/ | |
/****************************************************** | |
* Testing custom curve functions | |
******************************************************/ | |
draw.move(10, 100); | |
draw.curve(40, 10, 100, 100); | |
function Draw() { | |
/****************************************************** | |
* Create canvas to draw elements | |
******************************************************/ | |
var canvas, context; | |
canvas = document.createElement("canvas"); | |
canvas.setAttribute("width", window.innerWidth); | |
canvas.setAttribute("height", window.innerHeight); | |
canvas.setAttribute("style", "position: absolute; x:0; y:0;"); | |
document.body.appendChild(canvas); | |
context = canvas.getContext(2d), | |
this.curve = function() { | |
}; | |
this.line = function() { | |
} | |
function Curve(PointStart, PointControl, PointEnd) { | |
/****************************************************** | |
* Declare private vars | |
******************************************************/ | |
var | |
time, time1, time2, pointStart, pointControl, pointEnd, constraint1, constraint2, pointDraw, smooth1 = 1, | |
smooth2 = 1; | |
time1 = (1 + smooth2) / 2; | |
time2 = (1 - smooth1) / 2; | |
constraint1 = time≤time1 ? add(pointStart, multiply(time / time1, add((pointControl, -pointStart)))) : pointControl | |
constraint2 = time≥time2 ? add(pointControl, multiply(time - time2) / (1 - time2), add(pointEnd, -pointControl))): pointControl | |
pointDraw = add(constraint1, multiply(time, add(constraint2, -constraint1))); | |
// If time is between 0-1 then point is on the curve. I think! | |
// if time<=time1 | |
} | |
Point.count = 0; | |
function Point() { | |
var x = 0, | |
y = 0; | |
this.name = "Point" + (Point++); | |
this.move = function(X, Y) { | |
x = X; | |
y = Y; | |
context.beginPath(); | |
context.arc(x, y, 20, 0, Math.PI * 2); | |
context.fill(); | |
} | |
} | |
function drawPixel(x, y) { | |
context.fillRect(x, y, 1, 1); | |
} | |
function drawPoint(x, y) { | |
} | |
function add(value1, value2) { | |
return { | |
"x": value1.x + value2.x, | |
"y": value1.y + value2.y | |
} | |
} | |
function multiply(value1, value2) { | |
if (value1 instanceof Object) { | |
return { | |
"x": value1.x * value2.x, | |
"y": value1.y * value2.y | |
} | |
} | |
return { | |
"x": value1 * value2.x, | |
"y": value1 * value2.y | |
} | |
} | |
function Line() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment