-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
83 lines (65 loc) · 2.48 KB
/
Copy pathscript.js
File metadata and controls
83 lines (65 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var canvasWidth;
var canvasHeight;
var canvas;
var draw;
$(function () {
canvasWidth = $(window).width();
// Canvas was extending beyond the browser window for whatever reason
// band-aid fix
canvasHeight = $(window).height()-($(".form-row").height() + 12);
canvas = $("canvas");
draw = canvas[0].getContext('2d');
canvas.attr({"width": canvasWidth, "height": canvasHeight});
drawFunction();
$(".advanced").hide();
});
// Function that maps a point in a specific range to a new point in
// another range
function convertToRange(min, max, newMin, newMax, point) {
var m = (newMax-newMin)/(max-min);
return (((m * point) + newMin) - (m * min));
}
function getTrigFunctionOf(value) {
var a = parseFloat($("#a").val());
var b = parseFloat($("#b").val());
var c = parseFloat($("#c").val());
var d = parseFloat($("#d").val());
var trigFunction = $("#function").val();
// If the trigFunction matches 'sin'
if (!trigFunction.localeCompare("sin")) {
return (a*(Math.sin(b*(value-c)))) + d;
} else {
return (a*(Math.cos(b*(value-c)))) + d;
}
}
function drawFunction() {
// TODO: Check these values are actual numbers
var rangeMin = parseFloat($("#rangeMin").val());
var rangeMax = parseFloat($("#rangeMax").val());
var domMin = parseFloat($("#domMin").val());
var domMax = parseFloat($("#domMax").val());
// Accuracy is how many lines are drawn in total
var accuracy = $("#accuracy").val();
draw.clearRect(0, 0, canvas[0].width, canvas[0].height);
draw.beginPath();
for (var i = 0; i <= accuracy; i++) {
// Divide the range up into equal parts, and draw lines to these points and their
// sine values
var point_x = i * (canvasWidth/accuracy);
// First map the current x position to where it would be in the range [domMin, domMax],
// find the sine value of this newly mapped value, and then map the sine value
// to a value in the range of the canvas.
var point_y = convertToRange(rangeMin, rangeMax, 0, canvasHeight, getTrigFunctionOf(convertToRange(0, canvasWidth, domMin, domMax, point_x)));
// If this is the first point, it is at the origin and thus there is no line
// to draw yet
if (i === 0) {
draw.moveTo(point_x, point_y);
} else {
draw.lineTo(point_x, point_y);
}
}
draw.stroke();
}
function toggleAdvancedSettings() {
$(".advanced").slideToggle("slow");
}