forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton-particle.html
More file actions
173 lines (145 loc) · 5.43 KB
/
Copy pathbutton-particle.html
File metadata and controls
173 lines (145 loc) · 5.43 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
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
165
166
167
168
169
170
171
172
173
<!DOCTYPE html>
<html>
<head>
<title>PlayCanvas Particle Button</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link rel="icon" type="image/png" href="../playcanvas-favicon.png" />
<script src="../../build/playcanvas.js"></script>
<script src="../../build/playcanvas-extras.js"></script>
<style>
body {
margin: 0;
overflow-y: hidden;
}
</style>
</head>
<body>
<!-- The canvas element -->
<canvas id="application-canvas"></canvas>
<!-- The script -->
<script>
var canvas = document.getElementById("application-canvas");
// Create the application and start the update loop
var app = new pc.Application(canvas, {
mouse: new pc.Mouse(document.body),
touch: new pc.TouchDevice(document.body),
elementInput: new pc.ElementInput(canvas)
});
app.start();
// Set the canvas to fill the window and automatically change resolution to be the same as the canvas size
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
window.addEventListener("resize", function () {
app.resizeCanvas(canvas.width, canvas.height);
});
var miniStats = new pcx.MiniStats(app);
// Create a camera
var camera = new pc.Entity();
camera.addComponent("camera", {
clearColor: new pc.Color(0, 0, 0)
});
app.root.addChild(camera);
// Create a 2D screen
var screen = new pc.Entity();
screen.addComponent("screen", {
referenceResolution: new pc.Vec2(1280, 720),
scaleBlend: 0.5,
scaleMode: pc.SCALEMODE_BLEND,
screenSpace: true
});
app.root.addChild(screen);
// Create a simple button
var button = new pc.Entity();
button.addComponent("button", {
imageEntity: button
});
button.addComponent("element", {
anchor: [ 0.5, 0.5, 0.5, 0.5 ],
color: new pc.Color(0.4, 0.4, 0.4),
height: 40,
pivot: [ 0.5, 0.5 ],
type: pc.ELEMENTTYPE_IMAGE,
width: 175,
useInput: true
});
screen.addChild(button);
// Create a label for the button
var label = new pc.Entity();
label.addComponent("element", {
anchor: [ 0.5, 0.5, 0.5, 0.5 ],
color: new pc.Color(1, 1, 0),
fontSize: 36,
height: 64,
pivot: [ 0.5, 0.5 ],
text: "LABEL",
type: pc.ELEMENTTYPE_TEXT,
width: 128,
wrapLines: true
});
button.addChild(label);
// Load a font
var fontAsset = new pc.Asset('courier.json', "font", {url: "../assets/fonts/courier.json"});
fontAsset.on('load', function () {
// Apply the font to the text element
label.element.fontAsset = fontAsset;
});
// load spark texture for particle system
app.assets.loadFromUrl('../assets/textures/spark.png', 'texture', function (err, asset) {
// Create entity for particle system
var particles = new pc.Entity();
// insert sparks as a child of the button, but before Label - that is the order for rendering
button.insertChild(particles, 0);
// particles will render in UI layer
var UILayer = app.scene.layers.getLayerByName("UI");
// particle size
var scaleCurve = new pc.Curve(
[0, 0.03]
);
// color changes throughout lifetime
var colorCurve = new pc.CurveSet([
[0, 1, 0.25, 1, 0.375, 0.5, 0.5, 0],
[0, 0, 0.125, 0.25, 0.25, 0.5, 0.375, 0.75, 0.5, 1],
[0, 0, 1, 0]
]);
// increasing gravity to get them to move
var worldVelocityCurve = new pc.CurveSet([
[0, 0],
[0, 0, 0.1, 0.1, 0.1, -0.1],
[0, 0]
]);
// rotate sparks 360 degrees per second
var angleCurve = new pc.Curve(
[0, 360]
);
// when texture is loaded add particlesystem component to entity
particles.addComponent("particlesystem", {
numParticles: 100,
lifetime: 1,
rate: 0.01,
// make them follow the buttn in screen-space
localSpace: true,
screenSpace: true,
emitterShape: pc.EMITTERSHAPE_SPHERE,
emitterRadius: 100,
scaleGraph: scaleCurve,
rotationSpeedGraph: angleCurve,
colorGraph: colorCurve,
velocityGraph: worldVelocityCurve,
colorMap: asset.resource,
layers: [UILayer.id]
});
// sort all screen elements
screen.screen.syncDrawOrder();
});
var time = 0, previousTime;
app.on("update", function (dt) {
time += dt * 0.3;
// move buttons along the circular path
button.setLocalPosition(300 * Math.sin(time), 300 * Math.cos(time), 0);
});
app.assets.add(fontAsset);
app.assets.load(fontAsset);
</script>
</body>
</html>