-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcanvasPractice.js
More file actions
106 lines (94 loc) · 2.36 KB
/
Copy pathcanvasPractice.js
File metadata and controls
106 lines (94 loc) · 2.36 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
var cview ;
var picRedSize = 80; //Percent of reduced size as whole number for %
var shapes = new Array();
var shapeCT = 0;
var startx=0;
var starty=0;
var cwidth=630;
var cheight=460;
function CObject(x,y) {
this.x = x;
this.y = y;
//this.shape = shape;
CObject.prototype.x;
CObject.prototype.y;
//CObject.prototype.shape;
CObject.prototype.draw= function() {
if (this.x < 10)
{
cview.fillRect(this.x, this.y, 20,20);
}
else
{
cview.fillRect(this.x-10, this.y-10, 20,20);
}
}
}
function canvasonclick(e)
{
//Find out where the click happened
var x;
var y;
if (e.pageX != undefined && e.pageY != undefined)
{
x = e.pageX;
y = e.pageY;
}
else
{
x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
x -= canvas.offsetLeft;
y -= canvas.offsetTop;
x -= startx;
tmp = new CObject(x,y);
tmp.draw();
shapes[shapeCT]= tmp;
shapeCT++;
}
function setsize()
{
//Info can be found at: http://www.javascripter.net/faq/browserw.htm
if (document.body && document.body.offsetWidth) {
cwidth = document.body.offsetWidth;
cheight = document.body.offsetHeight;
}
if (document.compatMode=='CSS1Compat' &&
document.documentElement &&
document.documentElement.offsetWidth ) {
cwidth = document.documentElement.offsetWidth;
cheight = document.documentElement.offsetHeight;
}
if (window.innerWidth && window.innerHeight) {
cwidth = window.innerWidth ;
cheight = window.innerHeight ;
}
maxWidth = cwidth;
maxHeight = 2 * cheight;
cwidth = Math.floor(cwidth *(picRedSize/100.0));
cheight = Math.floor(cheight * (picRedSize/100.0));
canvas.setAttribute('height', cheight);
canvas.setAttribute('width', cwidth);
}
function init()
{
//Create canvas to draw image to:
canvas = document.getElementById('canvas');
setsize();
if (canvas.addEventListener)
{
canvas.addEventListener("click", canvasonclick, false);
//canvas.addEventListener("mouseover", makeVisible, false);
//canvas.addEventListener("mouseout", makeInvisible, false);
}
else
{
canvas.attachEvent("onclick", canvasonclick );
//canvas.attachEvent("onmouseover", makeVisible);
//canvas.attachEvent("onmouseout", makeInvisible);
}
cview = canvas.getContext("2d");
var rect = new CObject(25,25);
rect.draw();
}