bubble-hearts
(<3kb) 💖Bubble hearts animation.(Canvas 实现直播间点赞动画)
What's it?
- Let images bubble in canvas, like hearts (etc).
- It's a familiar animation in live room.
- Live Demo
Getting started
$ npm install --save-dev bubble-hearts
Usage
#Init
let stage = ;
#Config
let canvas = stagecanvas;let context = stagecontext;canvaswidth = 200;canvasheight = 400;canvasstyle'width' = '200px';canvasstyle'height' = '400px';
#Display
documentbody;
#Animate
let image = ;image { stage;};imagesrc = src;
Doc
#bubble
stage.bubble( image : Image/Canvas, duration : Number, handler : Function )
Param | Type | Required | Default | Description |
---|---|---|---|---|
image | Image/Canvas | * | - | Let this image bubbles in canvas. |
duration | Number | - | 3000 | The duration of animation. |
handler | Function | - | @see handler | The handler of animation. |
#handler
handler( lifespan : Number )
Param | Type | Description |
---|---|---|
lifespan | Number | From 1 to 0 ; 1 means full live; 0 means over. |
- default handler
/** * Create a default Render * @param * @param * @return */ { const zoomInStage = random / 100; const zoomInRest = 1 - zoomInStage; const basicScale = random + random / 100; let { if lifespan > zoomInStage return Math * basicScale; else return basicScale; }; const basicRotate = random; let { return basicRotate; }; const offset = 10; const basicTranslateX = canvaswidth / 2 + random; const amplitude = canvaswidth - Math / 2 - offset; const wave = random * random ? 1 : -1; const frequency = random; let { if lifespan > zoomInStage return basicTranslateX; else return basicTranslateX + wave * Math; }; let { return imageheight / 2 + canvasheight - imageheight / 2 * lifespan; }; const fadeOutStage = random / 100; let { if lifespan > fadeOutStage return 1; else return 1 - fadeOutStage - lifespan / fadeOutStage; }; return { if lifespan >= 0 let scale = ; let rotate = ; let translateX = ; let translateY = ; context; context; context; contextglobalAlpha = ; context; context; context; context; contextglobalAlpha = 1; else return true; };}
- simple custom example
stage;
return true
?
#why - lifespan from
1
to0
, and go on to be negative. - handler function repeats again and again to
drawImage
. return true
is a flag to tell iterator to remove this handler, and stop repeating this handler again.
License
MIT