-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawing.js
150 lines (133 loc) · 3.66 KB
/
Drawing.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var drawLine = false;
var oldCoor = {};
var i = 1;
var t;
var coordinateList = [];
var lastCoordinate = {};
var imageObj = new Image();
imageObj.onload = function() {
context.drawImage(imageObj, 0, 0, 300, 380);
};
imageObj.src = 'images/RedField.png';
$(document).ready(function(){
orangePersist.initializeApp();
console.log("GETTING USERNAME");
$("#userName").val(localStorage.getItem("userName"));
});
function saveUserName(){
console.log("SETTING USERNAME");
localStorage.setItem("userName", $("#userName").val());
}
function clearPath(){
context.clearRect(0, 0, 300, 330);
context.drawImage(imageObj, 0, 0, 300, 380);
coordinateList = [];
lastCoordinate = {};
}
function addCoordinate(coor){
coordinateList.push(coor);
}
function updateRobotHTML(){
}
function randomColor(){
var choices = "0123456789abcdef";
var out = "#";
for(var i = 0; i < 6; i++){
out += choices[Math.floor(Math.random() * 16)];
}
return(out);
}
function adjustCanvas(){
$("#canvasHolder").css('height' , $(window).height()-25);
$("#canvasHolder").css('height' , $(window).height()-25);
$("#main").attr('width' , $("#canvasHolder").width());
$("#main").attr('height' , $("#canvasHolder").height());
}
function resize(){
//$("#main").outerHeight($(window).height()-$("#main").offset().top- Math.abs($("#main").outerHeight(true) - $("#main").outerHeight()));
//$("#main").outerHeight(100*i);
//$("#main").outerWidth(100*i);
canvas.width = $(window).width() - 35;
canvas.height = $(window).height() - 25;
}
//$(document).ready(function(){
$.material.init()
//resize();
adjustCanvas();
$(window).on("resize", function(){
//resize();
adjustCanvas();
});
context.stroke();
//$("#main")[0].webkitRequestFullScreen(Element.ALLOW_KEYBOARD_INPUT); //Chrome
//$("#main")[0].mozRequestFullScreen(); //Firefox
canvas.addEventListener('touchmove', movePath, false);
canvas.addEventListener('touchstart', startPoint, false);
canvas.addEventListener('touchend', endPoint, false);
canvas.addEventListener('mousemove', movePath, false);
canvas.addEventListener('mousedown', startPoint, false);
canvas.addEventListener('mouseup', endPoint, false);
//});
function getMousePos(canvas, evt) {
var rect = canvas.getBoundingClientRect();
var evtType = evt.constructor.name;
if(evtType == "TouchEvent"){
return {
x: evt.touches[0].clientX - rect.left,
y: evt.touches[0].clientY - rect.top
};
}
else if(evtType = "MouseEvent"){
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
else{
alert("Input type not supported!")
}
}
function drawPoint(context , x , y){
context.fillRect(x,y);
}
function drawPointLines(context , point){
var color = "#FFFFFF";
if(lastCoordinate.length == 0){
lastCoordinate = point;
}
else{
context.beginPath();
context.strokeStyle = color;
context.moveTo(lastCoordinate[0] , lastCoordinate[1]);
context.lineTo(point[0] , point[1]);
addCoordinate(point);
lastCoordinate = point;
context.stroke();
}
}
function movePath(evt){
t = evt;
if(drawLine){
var mousePos = getMousePos(canvas, evt);
var message = mousePos.x + ' , ' + mousePos.y;
//drawPoint(context , mousePos.x , mousePos.y);
drawPointLines(context , [mousePos.x , mousePos.y]);
console.log(message);
}
evt.preventDefault();
return false;
}
function startPoint(evt){
console.log("A");
drawLine = true;
evt.preventDefault();
return false;
}
function endPoint(evt){
console.log("B");
drawLine = false;
evt.preventDefault();
return false;
}