/    Sign up×
Community /Pin to ProfileBookmark

js canvas performant reflection

Hi there.

Let’s say I have some animations in a canvas, and I want to have the animations reflected (like as if the animations are hovering over a glass surface of sorts).

First idea I had: I thought “no problem, lets just distort and copy some stuff within the canvas (= a circle gets calculated again as an ellipse)”, but that was quickly getting too troublesome.

Second idea: “let’s just create a second canvas, copy the contents of the first canvas into the second, and perspectively distort it with css”. That was a lot easier and looked nice, but with the reduced opacity and such the FPS dropped on an old MacBook to just about 25FPS.

Third idea: “let’s turn the content of the canvas into an image and apply the CSS to that, as having an image and a canvas might be more performant than having two canvases”. Did that, looked the same, but the FPS dropped to 22.

I guess another possibility would be to use three.js or babylon.js to create the animation in a 3d environment and add a reflective surface. But I’m unsure if this would improve the performance.

If anybody would have any ideas how to achieve this effect in a more performant way, that would be great. 🙂 Thanks.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumMar 23.2020 — Another approach comes to my mind which is a combination of your first and second idea: Do not draw the stuff again as in your first idea but take the bitmap of the first drawing and perform the perspective transformation and other modifications when drawing it again. If you can do without dithering some basic mathemathics should be sufficient to do so. However I don't know about the performance aspect.

When googling for perspective transformations I found this Pen:

https://codepen.io/kaliedarik/pen/MwPVOR

Works smooth when dragging, thus the performance might be quite fine. Obviously uses an external library (scrawl).
×

Success!

Help @Fnorbl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...