Skip to content

Commit ab63558

Browse files
committed
gradient fills now available!
1 parent e9d4cf6 commit ab63558

2 files changed

Lines changed: 79 additions & 9 deletions

File tree

Animation.js

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,47 @@ class AnimatedPath
163163
* @param {*} t
164164
* @returns
165165
*/
166-
static tweenFill(a,b,t)
166+
static tweenFill(ctx, a,b,t)
167167
{
168-
if(typeof a == "string")
168+
if(typeof a == "string" && typeof b == "string")
169169
{
170170
return AnimatedPath.tweenHex(a,b,t);
171171
}
172+
if(a.type)
173+
{
174+
let graddef = AnimatedPath.tweenGradient(a,b,t);
175+
switch(graddef.type)
176+
{
177+
case "linear":
178+
{
179+
let grad = ctx.createLinearGradient(...graddef.coords);
180+
for(let i=0;i<graddef.stops.length;i++)
181+
{
182+
grad.addColorStop(graddef.stops[i],graddef.colours[i]);
183+
}
184+
return grad;
185+
}
186+
}
187+
}
172188

173189
}
190+
static tweenGradient(a,b,t)
191+
{
192+
let finalcoords=[];
193+
let finalcolours=[];
194+
a.coords.forEach((val,i)=>{
195+
finalcoords.push(a.coords[i]+(b.coords[i]-a.coords[i])*t);
196+
});
197+
a.colours.forEach((val,i)=>{
198+
finalcolours.push(AnimatedPath.tweenHex(a.colours[i],b.colours[i],t));
199+
});
200+
return {
201+
type: a.type,
202+
coords:finalcoords,
203+
stops:a.stops,
204+
colours:finalcolours
205+
};
206+
}
174207
/**
175208
* Tweens between two hex colours
176209
* @param {string} a first colour as #RRGGBB
@@ -184,6 +217,13 @@ class AnimatedPath
184217
{
185218
return "#000000";
186219
}
220+
let aa = 255;
221+
let ba = 255;
222+
if(a.length>=9)
223+
{
224+
aa=parseInt(a.substring(7,9),16);
225+
ba=parseInt(b.substring(7,9),16);
226+
}
187227
let ar = parseInt(a.substring(1,3),16);
188228
let ag = parseInt(a.substring(3,5),16);
189229
let ab = parseInt(a.substring(5,7),16);
@@ -193,18 +233,20 @@ class AnimatedPath
193233
let R = ar + (br-ar)*t;
194234
let G = ag + (bg-ag)*t;
195235
let B = ab + (bb-ab)*t;
196-
return AnimatedPath.rgbToHex(Math.floor(R), Math.floor(G), Math.floor(B));
236+
let A = aa + (ba-aa)*t;
237+
return AnimatedPath.rgbToHex(Math.floor(R), Math.floor(G), Math.floor(B), Math.floor(A));
197238
}
198239
/**
199240
* Converts 3 values into hexadecimal RGB
200241
* @param {number} r
201242
* @param {number} g
202243
* @param {number} b
244+
* @param {number} a
203245
* @returns
204246
*/
205-
static rgbToHex(r, g, b)
247+
static rgbToHex(r, g, b, a=255)
206248
{
207-
return "#" + AnimatedPath.componentToHex(r) + AnimatedPath.componentToHex(g) + AnimatedPath.componentToHex(b);
249+
return "#" + AnimatedPath.componentToHex(r) + AnimatedPath.componentToHex(g) + AnimatedPath.componentToHex(b) + AnimatedPath.componentToHex(a);
208250
}
209251
/**
210252
* Converts a single value to hex.
@@ -234,7 +276,11 @@ class AnimatedPath
234276
frames.forEach((f)=>{
235277
// also save the paths in parallel for processing
236278
paths.push(f.path);
237-
result.frames.push({"time": f.time, "fill":f.fill,"rotation":(f.rotation ?? 0)});
279+
result.frames.push({
280+
"time": f.time,
281+
"fill":f.fill,
282+
"rotation":(f.rotation ?? 0)
283+
});
238284
});
239285
let chunked = [];
240286
// chunk the paths
@@ -261,7 +307,6 @@ class AnimatedPath
261307
"values": f0.values
262308
});
263309
result.length = 1;
264-
console.log(result);
265310
}
266311
return result;
267312
}
@@ -378,7 +423,7 @@ class VectorAnimation
378423
// generate the SVG path commands from the path and the keyframe values
379424
let strpath = AnimatedPath.stitch(path.path, values);
380425
// tween fill
381-
let fill = AnimatedPath.tweenFill(frame.fa, frame.fb, frame.t);
426+
let fill = AnimatedPath.tweenFill(ctx,frame.fa, frame.fb, frame.t);
382427
// render the path in its fill colour
383428
ctx.fillStyle = fill;
384429
// rotate

gameObjects/Projectile.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,41 @@ class Projectile extends GameObject
1111
Damage dealt by this projectile on hitting a target
1212
*/
1313
damage = 4;
14+
test1 = {
15+
"idle": [[
16+
{
17+
fill: "#FFFFFF",
18+
time: 0.0,
19+
path: "M 0 30 C -15 -20 15 -20 0 30 z"
20+
}
21+
]]
22+
};
23+
test2 = {
24+
"idle": [[
25+
{
26+
fill: {
27+
type: "linear",
28+
coords: [0, -5, 0, 30],
29+
stops: [0,1],
30+
colours: ["#FFFF00FF","FF780030"]
31+
},
32+
time: 0.0,
33+
path: "M 0 30 C -15 -20 15 -20 0 30 z"
34+
}
35+
]]
36+
};
1437
constructor()
1538
{
1639
super("bullet");
1740
// default oblong projectile
1841
this.hitbox = new Rectangle(-5,-5,10,30);
1942
this.originalHitbox = new Rectangle(-5,-5,10,30);
20-
43+
this.sprite=VectorSprite.fromRawObject(this.test2);
2144
}
2245
draw(ctx)
2346
{
47+
super.draw(ctx);
48+
return;
2449
// define a trail fading from yellow to orange
2550
// #TODO: something less hardcoded
2651
const grad = ctx.createLinearGradient(this.x,this.y-5,this.x,this.y+30);

0 commit comments

Comments
 (0)