-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
165 lines (138 loc) · 5.22 KB
/
index.html
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jCircle - Circular interactions plugin for jQuery</title>
<style>
#content{width:400px; margin:0px auto 0px auto;}
#circular1{ height:400px;width:400px;background-color:red;}
#circular2{ height:400px;width:400px;background-color:grey;}
#circularDebug{ height:400px;width:400px;background-color:blue; color:white;}
</style>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="/js/mylibs/jCircle.js"></script>
<script>
var view = function(num){
$('.examples').hide();
$('#example' + parseInt(num)).show();
}
</script>
</head>
<body>
<div id="content">
<h2>jCircle.js</h2>
<p> A <a href="http://jquery.com/">jQuery</a> <a href="http://plugins.jquery.com/">plugin</a> to enable circular UI interactions, like click & drag to rotate. View Source for more examples. <a href="/js/mylibs/jCircle.js">Get the plugin</a>.</p>
<p>Beta code: only works in newest browsers with good HTML5 support.</p>
<pre>
$('#id').jCircle(
function(cursorRadian, smoothedRadian, velocity, direction)
{
//do things with the values, like rotate an image
}
);
</pre>
<p>
<a href="javascript: view(1);">Example 1 (Follow)</a>
<a href="javascript: view(2);">Example 2 (Knob)</a>
<a href="javascript: view(3);">Example 3 (Debug)</a>
</p>
<div id="example1" class="examples" style="display:none">
<div id="circular1"><canvas id="canvas1" height="400" width="400"></canvas></div>
</div>
<div id="example2" class="examples">
<div id="circular2"><canvas id="canvas2" height="400" width="400"></canvas></div>
<label for="circular2output">Knob Percent:</label>
<input type="text" id="circular2output" name="circular2output" value="0">
<br>
<label for="circularSmoothed">Knob Radian:</label>
<input type="text" id="circularSmoothed" name="circularSmoothed" value="0">
<br>
<label for="circular2output">Cursor Radian:</label>
<input type="text" id="circular2cursor" name="circular2cursor" value="0">
</div>
<div id="example3" class="examples" style="display:none">
<div id="circularDebug">zxcv</div>
</div>
<p>1/29/13: Smoothest in Chrome. Good in Firefox, including mobile. Android default browser crashes, doesn't work in iOS Safari.</p>
<h2><a href="/">Home</a></h2>
</div>
<script>
//example 1 uses canvas
(function(){
var canvas = document.getElementById('canvas1')
, context = canvas.getContext('2d')
, rectWidth = 150
, rectHeight = 75
, translatedX = canvas.width/2
, translatedY = canvas.height/2;
// translate context to center of canvas and draw a new rectangle
var drawBox = function(radian, smoothedRadian, velocity, direction) {
canvas.width = canvas.width;
context.translate( translatedX, translatedY);
context.rotate(radian);
context.fillStyle = 'blue';
context.fillRect(rectWidth/-2, rectHeight/-2, rectWidth, rectHeight);
};
//setup the first box, independent of the jCircle redraw
drawBox(0);
//setup example 1's jCircle event and callback
//this uses radian (mouse position) so it jumps when you re-click elsewhere
$('#circular1').jCircle( drawBox );
}());//end example 1
//example 2 also uses canvas
(function(){
var canvas = document.getElementById('canvas2')
, context = canvas.getContext('2d')
, imgWidth = 150
, imgHeight = 150
, translatedX = canvas.width/2
, translatedY = canvas.height/2;
var imageObj = new Image();
var drawImg;
// callback for Image.onload
var drawImage = function( imageObj ){
//drawImg closure used by jCircle's callback to redraw the knob, rotated
drawImg = function( rotationRads ){
canvas.width = canvas.width;
context.translate( translatedX, translatedY);
context.rotate( rotationRads );
context.drawImage( imageObj, imgWidth/-2, imgHeight/-2);
};
drawImg(0);
}
imageObj.onload = function(){
//setup the first box, and provide the imgData for drawing in the drawImg closure
drawImage(this);
};
imageObj.src = '/knob.png';
//setup example 2's jCircle event and callback
//this example uses the smoothedRadian, which is ignorant of the current event's position (radian)
//so you can smoothly tweak the knob's position
var $circularOut = $('#circular2output');
var $circularCursor = $('#circular2cursor');
var $circularSmoothed = $('#circularSmoothed');
$('#canvas2').jCircle(
function(radian, smoothedRadian, velocity, direction) {
//output the value from 0 - 100
var maxValues = 101;
$circularOut[0].value = Math.floor((smoothedRadian / (Math.PI*2))*maxValues);
//output the radian of the cursor
$circularCursor[0].value = radian;
//output the radian of the cursor
$circularSmoothed[0].value = smoothedRadian;
//note that this is drawImg: NOT drawImage, which created drawImg
drawImg(smoothedRadian);
}
);
}());//end example 2
//the third example, just outputs the values
(function(){
$('#circularDebug').jCircle(
function(radian, smoothedRadian, velocity, direction) {
$('#circularDebug').text("direction: "+direction +" radian: "+ radian+" smradian: "+ smoothedRadian+" velocity: "+ velocity);
}
);
}());//end example 3
</script>
</body>
</html>