-
-
Notifications
You must be signed in to change notification settings - Fork 405
Add confetti.shapesFromImage method to create confetti from image source #243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -857,6 +857,39 @@ | |
}; | ||
} | ||
|
||
function shapesFromImage(imageData) { | ||
var scalar = imageData.scalar != null ? imageData.scalar : 1; | ||
var size = 10 * scalar; | ||
|
||
var img = new Image(); | ||
img.src = imageData.src; | ||
|
||
return promise(function (resolve) { | ||
Comment on lines
+860
to
+867
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, so I know why you did this. It is so convenient for this lib to just load the image for you, even from a URL. I've seen this backfire a lot in libraries though, from folks who don't totally understand how image loading works thinking that the library has a bug when their images are not served as expected. Not to mention, the image here doesn't include error handling (because why not add two problems to one comment, am I right?) Anyway, I think a much more flexible API would be |
||
img.addEventListener('load', function() { | ||
var sprites = imageData.sprites != null | ||
? imageData.sprites | ||
: [{ x: 0, y: 0, width: img.naturalWidth, height: img.naturalHeight }]; | ||
|
||
var baselineWidth = Math.max.apply(null, sprites.map(function(rect) { return rect.width; })); | ||
|
||
resolve(sprites.map(function(sprite) { | ||
var width = size * sprite.width / baselineWidth; | ||
var height = size * sprite.height / baselineWidth; | ||
var scale = 1 / scalar; | ||
|
||
var canvas = new OffscreenCanvas(width, height); | ||
canvas.getContext('2d').drawImage(img, sprite.x, sprite.y, sprite.width, sprite.height, 0, 0, width, height); | ||
|
||
return { | ||
type: 'bitmap', | ||
bitmap: canvas.transferToImageBitmap(), | ||
matrix: [scale, 0, 0, scale, -width * scale / 2, -height * scale / 2] | ||
}; | ||
})); | ||
Comment on lines
+875
to
+888
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, so some info about the original sprite issue, since I know I didn't document much in the original tasks. Once upon a time, there was the crazy idea of rendering emoji. This was pretty incompatible with how rectangles and circles are rendered -- since those are point-based, we can calculate where the points would exist if a 3D transformation was applied, and then do very single (read: cheap) 2D objects with silly points. However, at the time, neither text nor images seemed like they were going to be that simple. They are not point based, so I can't just manipulate their points. At the time, the only method I could find render emoji was to use transforms on the canvas, so rather than applying a 3D effect on the confetti, the 3D effect is applied to the canvas and the confetti was drawn normally. This is incredibly expensive and performance was abysmal, so I did not want to put in in production. I know that in game rendering (at least back in the day), it was not unheard of to have a sprite for any given asset, and that sprite is many many different 2D images of that resource from all possible angles. This means any time you need to render that asset, you just pick the correct angle out of the sprite -- super cheap. So by saying "support sprites", what I meant was support a way to pre-render the entire animation sequence for a single piece of confetti, and then render each frame as a portion of that sprite. That is all a long-winded way (sorry) of saying: sprites here complicate the API without delivering any benefit. I would not support them... at least not this version. A user just as well could create multiple shapes from multiple separate images (or honestly, just create the shape in a loop and apply cropping each time if for some reason they must load a sprite image of completely separate objects). |
||
}); | ||
}); | ||
} | ||
|
||
module.exports = function() { | ||
return getDefaultFire().apply(this, arguments); | ||
}; | ||
|
@@ -866,6 +899,7 @@ | |
module.exports.create = confettiCannon; | ||
module.exports.shapeFromPath = shapeFromPath; | ||
module.exports.shapeFromText = shapeFromText; | ||
module.exports.shapesFromImage = shapesFromImage; | ||
}((function () { | ||
if (typeof window !== 'undefined') { | ||
return window; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -816,6 +816,74 @@ test('[text] shoots confetti of an emoji shape', async t => { | |
t.is(t.context.image.hash(), 'cPpcSrcCjdC'); | ||
}); | ||
|
||
const shapeFromImageImage = async (page, args) => { | ||
const { base64png, ...shape } = await page.evaluate(` | ||
Promise.resolve().then(async () => { | ||
const [{ bitmap, ...shape }] = await confetti.shapesFromImage(${JSON.stringify(args)}); | ||
|
||
const canvas = document.createElement('canvas'); | ||
canvas.width = bitmap.width; | ||
canvas.height = bitmap.height; | ||
const ctx = canvas.getContext('2d'); | ||
ctx.drawImage(bitmap, 0, 0, bitmap.width, bitmap.height); | ||
|
||
return { | ||
...shape, | ||
base64png: canvas.toDataURL('image/png') | ||
}; | ||
}); | ||
`); | ||
|
||
return { | ||
...shape, | ||
buffer: base64ToBuffer(base64png) | ||
}; | ||
}; | ||
|
||
test('[image] shapesFromImage renders an image', async t => { | ||
const page = t.context.page = await fixturePage(); | ||
|
||
const { buffer, ...shape } = await shapeFromImageImage(page, { src: 'data:image/gif;base64,R0lGODlhBQAFAIABAP8AAAAAACH5BAEKAAEALAAAAAAFAAUAAAIIjA+RwKxuUigAOw', scalar: 2 }); | ||
|
||
t.context.buffer = buffer; | ||
t.context.image = await readImage(buffer); | ||
|
||
t.deepEqual({ | ||
hash: t.context.image.hash(), | ||
...shape | ||
}, { | ||
type: 'bitmap', | ||
matrix: [0.5, 0, 0, 0.5, -5, -5], | ||
hash: '80anMEa00G0' | ||
}); | ||
}); | ||
|
||
// this test renders a black canvas in a headless browser | ||
// but works fine when it is not headless | ||
// eslint-disable-next-line ava/no-skip-test | ||
Comment on lines
+861
to
+863
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic of this test is copied from the corresponding emoji one, so I copied this comment over too, even though for me it seemed to render fine in headless. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not entirely surprising. That test was written quite a while ago, and I know a lot of work was done on headless mode since. It's possible both tests just work now. |
||
test('[image] shoots confetti of an image', async t => { | ||
const page = t.context.page = await fixturePage(); | ||
|
||
await page.evaluate(`Promise.resolve().then(async () => { | ||
window.__image = (await confetti.shapesFromImage({ src: 'data:image/gif;base64,R0lGODlhBQAFAIABAP8AAAAAACH5BAEKAAEALAAAAAAFAAUAAAIIjA+RwKxuUigAOw', scalar: 2 }))[0]; | ||
})`); | ||
|
||
// these parameters should create an image | ||
// that is the same every time | ||
t.context.buffer = await confettiImage(page, { | ||
startVelocity: 0, | ||
gravity: 0, | ||
scalar: 10, | ||
flat: 1, | ||
ticks: 1000, | ||
// eslint-disable-next-line no-undef | ||
shapes: [() => __image] | ||
}); | ||
t.context.image = await readImage(t.context.buffer); | ||
|
||
t.is(t.context.image.hash(), '9D_pL$p_Sr_'); | ||
}); | ||
|
||
/* | ||
* Custom canvas | ||
*/ | ||
|
@@ -1247,3 +1315,9 @@ test('[esm] exposed confetti method has a `shapeFromText` property', async t => | |
|
||
t.is(await page.evaluate(`typeof confettiAlias.shapeFromText`), 'function'); | ||
}); | ||
|
||
test('[esm] exposed confetti method has a `shapesFromImage` property', async t => { | ||
const page = t.context.page = await fixturePage('fixtures/page.module.html'); | ||
|
||
t.is(await page.evaluate(`typeof confettiAlias.shapesFromImage`), 'function'); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may be a moot point given my other comment about sprites, but I think this is a bad thing to demonstrate. SVG paths perform better than images (and text), so when you can you are far better off using
shapeFromPath
when at all possible. This demonstration looks like an endorsement to use SVGs as images.