Skip to content
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

User defined functions? #14

Open
amca01 opened this issue Feb 26, 2021 · 1 comment
Open

User defined functions? #14

amca01 opened this issue Feb 26, 2021 · 1 comment

Comments

@amca01
Copy link

amca01 commented Feb 26, 2021

Some of my graphics include grids, which can be easily programmed by sets of lines. However, it would be convenient to have a function called, say, "grid" which I can simply call when needed. For example, I have the following in the "head" part of my html document:

<script>
  function grid(nr,nc,h,w) {
  for (var i=0; i<= nr; i++ ) {
      stage.path().moveTo(0,i*h).lineTo(nc*w,i*h);
  }
  for (var i=0; i<=nc; i++ ) {
      stage.path().moveTo(i*w,0).lineTo(i*w,nr*h);
  }
  }
</script>

This creates a grid with a corner at (0,0), and with parameters as given. This is all very well, but what happens if I want to draw a grid elsewhere, or rotate a grid? I shouldn't have to include too many parameters in the function itself, and I'd hope that something like:

var g  = grid(2,4,100,50);
g.translate(200,100);

should work - but it doesn't. I also can't change the style of the grid lines using "stroke". Clearly I'm missing something.

How can I write a small function for a particular graphics element, which itself can be altered by chaining with standard graphics.js functions? Many thanks!

[I should say that I am moving some mathematics notes from LaTeX/PDF to html, using MathJax for the mathematics. And I think that graphics.js would be perfect for my diagrams - once I understand it better!]

@Shestac92
Copy link

@amca01
The GraphicsJS JS is an engine for drawing. It doesn't provide too many high-level functions and wrappers.
So, complex interactivity or high-level functions you should implement yourself.
For chaining, you should return an instance of which API you want to call futher.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants