I. INTRODUCTION
processingjs author is John Resig, this is the second masterpiece following jquery.
processingJS provides a programming language and operating environment for teaching visualization. By writing the processing program, teachers can display the complex physical, chemical and mathematical principles of the image to the students. such as drawing a variety of graphs, wave lines, particles, drawing molecular structure, of course, in the physiological health class can also draw a group of small tadpoles in swimming and other dynamic graphics.
Processing.js is an open programming language that enables program images, animations, and interactive applications without the use of Flash or Java applets.
Processing.js uses JavaScript to draw shapes sharp and manipulate HTML5 canvas elements to produce image animations.
Processing.js is lightweight, easy to understand mastered, and presents an ideal tool for visualizing data, creating user interfaces and developing Web-based games.
Two. Core functions
1234567891011121314151617181920212223242526272829303132333435 |
// Global variables 全局变量
int radius = 50.0;
int X, Y;
int nX, nY;
int delay = 16;
// Setup the Processing Canvas初始化设置
void setup(){
size( 200, 200 );
strokeWeight( 10 );
frameRate( 15 );
X = width / 2;
Y = width / 2;
nX = X;
nY = Y;
}
// Main draw loop 主要绘画函数功能
void draw(){
radius = radius + sin( frameCount / 4 );
// Track circle to new destination
X+=(nX-X)/delay;
Y+=(nY-Y)/delay;
// Fill canvas grey
background( 100 );
// Set fill-color to blue
fill( 0, 121, 184 );
// Set stroke-color white
stroke(255);
// Draw circle
ellipse( X, Y, radius, radius );
}
// Set circle‘s next destination 当用户鼠标在 Canvas移动时产生的action
void mouseMoved(){
nX = mouseX;
nY = mouseY;
}
|
Three. World's shortest clock code birth
123456 |
void draw() { size(200, 200);background(0); fill(80); noStroke(); ellipse(100, 100, 160, 160); stroke(255); line(100, 100, cos( TWO_PI*second()/60- HALF_PI) * 70 + 100, sin(TWO_PI*second()/60- HALF_PI) * 70 + 100); line(100, 100, cos( TWO_PI*minute()/60- HALF_PI) * 60 + 100, sin(TWO_PI*minute()/60- HALF_PI) * 60 + 100); line(100, 100, cos(TWO_PI*(hour()%12)/12- HALF_PI) * 50 + 100, sin(TWO_PI*(hour()%12)/12- HALF_PI) * 50 + 100); } |
It can be seen that code semantics is very strong, a circle, three lines, which is the framework to achieve one of the purposes.
Four. Complete code
12345678910111213141516 |
<!
DOCTYPE
html>
<
html
>
<
head
>
<
body
>
<
script
src="http://files.cnblogs.com/iamzhanglei/processing.js" type="text/javascript"></
script
>
<
script
type="application/processing">
void draw() {
size(200, 200);background(0); fill(80); noStroke(); ellipse(100, 100, 160, 160); stroke(255);
line(100, 100, cos( TWO_PI*second()/60- HALF_PI) * 70 + 100, sin(TWO_PI*second()/60- HALF_PI) * 70 + 100);
line(100, 100, cos( TWO_PI*minute()/60- HALF_PI) * 60 + 100, sin(TWO_PI*minute()/60- HALF_PI) * 60 + 100);
line(100, 100, cos(TWO_PI*(hour()%12)/12- HALF_PI) * 50 + 100, sin(TWO_PI*(hour()%12)/12- HALF_PI) * 50 + 100);
}
</
script
>
<
canvas
>你的浏览器不支持HTML5,请使用谷歌、IE9或者火狐浏览器··</
canvas
>
</
body
>
</
html
>
|
Five. Online Demo
The world's shortest clock code! Shorter, have the wood to have?