Giter Club home page Giter Club logo

Comments (8)

rezoner avatar rezoner commented on September 4, 2024

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.

rezoner avatar rezoner commented on September 4, 2024

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.

feiss avatar feiss commented on September 4, 2024

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.

rezoner avatar rezoner commented on September 4, 2024

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.

feiss avatar feiss commented on September 4, 2024

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.

rezoner avatar rezoner commented on September 4, 2024

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.

feiss avatar feiss commented on September 4, 2024

And you can sell it well: "Now with a brand new STARS operator!"

Powerful shit.

from canvasquery.

rezoner avatar rezoner commented on September 4, 2024

Pushed online

https://twitter.com/rezoner/status/562921309890703360

from canvasquery.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.