Comments (8)
In particualar I am nervously looking to replace the pattern that happens 24/7 in my code.
This is how you draw a centered image:
app.layer
.save();
.translate(100, 100));
.rotate(0.5 * Math.PI);
.scale(0.6, 0.6);
.drawImage(image, -image.width / 2, -image.height /2);
.restore();
from canvasquery.
First iteration - introducing align
:
This is how you draw a centered image:
app.layer
.align(100, 100, 0.5, 0.5)
.rotate(0.5 * Math.PI)
.scale(0.6, 0.6)
.drawImage(image, 0, 0)
.restore();
Essentials
app.layer
.save()
.translate(100, 100)
.drawImage(image, -image.width / 2, -image.height /2)
.restore();
/* equals to */
app.layer
.align(100, 100, 0.5, 0.5)
.drawImage(image, 0, 0)
.restore();
On a side note - I have also managed to decrease performance loss over raw canvas from 20% to 9%
from canvasquery.
looks great, the only issue I see here is that the programmer must know that a save() is being done, and he should do a restore() afterwards.
Also, someone could ask if there's a way to assure pixel perfect drawing (is there a floor() inside align?)
anyway, big fan of this align()
from canvasquery.
How about:
align
app.layer.save()
.translate(x, y)
.align(0.5, 0.5)
.rotate(0.5)
.scale(1.0, 1.0)
.drawImage(image, 0, 0)
.restore();
(because I find save
and translate
in align a bit shady)
stars
= save
, translate
, align
, rotate
, scale
app.layer
.stars(100, 100, 0.5, 0.5, 0.4, 1.0)
.drawImage(image, 0, 0)
.restore();
from canvasquery.
Nice, and undefined parameters could default to (0,0,0,0,0,1), so you could do stars(100,100) just to translate, stars(100,100,0.5,0.5,0.4) to rotate around center but not scale, etc..
clean and handy, do you like it?
from canvasquery.
Yesh I do.
I guess align
in the end would make more sense as you usually want to have just 0.5, 0.5
But then STARS makes it easy to remember the arguments order.
from canvasquery.
And you can sell it well: "Now with a brand new STARS operator!"
Powerful shit.
from canvasquery.
Pushed online
https://twitter.com/rezoner/status/562921309890703360
from canvasquery.
Related Issues (20)
- Are colors broken right now? HOT 2
- Demos or tutorials? HOT 1
- Version 0.9.1 HOT 1
- drawSprite missing HOT 1
- text size HOT 1
- Logitech Universal Reciever is incorrectly recognised as a gamepad HOT 1
- Old browsers compatibility requestAnimationFrame HOT 2
- preventContextMenu not working HOT 1
- Sprites in Playground HOT 15
- drawAtlasFrame() does not use x,y HOT 1
- Touchend is unreliable in multitouch HOT 4
- Integrating Playground with THREE.js
- Plugins in Playground HOT 1
- Mouselock Movement-Delta unprecise and IExplorer incompatible
- npm and browserify support HOT 14
- playground.Keyboard.prototype.keypress Event is an empty Function. HOT 7
- swapping color palettes
- cq.loadImages can not to use!
- Flip Sprite HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from canvasquery.