Search |
||
For Those About To RockPosted by malenkov on February 27, 2009 at 12:00 PM PST
This simple example produces the firework effect using JavaFX Script. The active use of random numbers brings variety to each firework volley. To generate random numbers I'm employing the import java.util.Random;
def random = Random {}
...
def r = 0 + 255 * random.nextFloat();
Have you paid attention to the The override function create() {
timer.play();
Group {
content: for (index in [1..18]) {
def length = 50 + 10 * random.nextFloat();
def radius = 2 + 2 * random.nextFloat();
for (delta in [1.0, 0.8, 0.6]) Circle {
fill: bind color
radius: delta * radius
centerX: bind delta * length * (1 - opacity)
transforms: Rotate {
angle: 20 * (index + random.nextFloat())
}
}
}
}
}
Several circles with proportional radius and length (the maximum distance from the centre) are created on each iteration of the cycle. All circles are drawn using a single color that varies sometimes. Therefore I applied data binding here: Now, consider key frames for animation. KeyFrame {
time: 0s
values: opacity => 0.0
action: function() {
def r = 0 + 255 * random.nextFloat();
def g = 100 + 155 * random.nextFloat();
def b = 50 + 205 * random.nextFloat();
color = Color.rgb(r, g, b);
translateX = 50 + random.nextFloat() * (scene.width - 100);
translateY = 50 + random.nextFloat() * (scene.height - 100);
}
}
When the animation cycle starts the KeyFrame {
time: 1s * random.nextFloat()
values: opacity => 1.0 tween Interpolator.DISCRETE
}
Randomly the KeyFrame {
time: 3s + 1s * random.nextFloat()
values: opacity => 0.0
}
Over approximately three seconds the KeyFrame {
time: 4s
}
This key frame is necessary to avoid volley desynchronization. It specifies the exact duration of the timeline. Stage{
title: "Firework (JavaFX sample)"
scene: Scene {
fill: Color.BLACK
width: 480
height: 320
content: for (index in [1..5]) Flash {
}
}
}
The previous code fragment adds several instances to the scene... et voilà!
For Those About To Rock (We Salute You)The source file of the example is available. My special thanks to AC/DC for the title. »
Related Topics >>
Programming Comments
Comments are listed in date ascending order (oldest first)
Submitted by pepejeria on Sun, 2009-03-01 05:14.
This demo takes way too long to load. Have the 1.6 update 12 installed.
Also, is it possible to avoid getting this popup saying "visit java.com"? Imagine if all plugins would do this...
|
||
|
|