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

Multiple whiskers #62

Merged
merged 5 commits into from
Jun 15, 2018
Merged

Conversation

paulsonnentag
Copy link
Contributor

Summary API Changes

as discussed in original issue: #49

Creation / Removal of whiskers

Currently, the API is missing a way to remove whiskers again. I would like to add a whisker only if the paper has something connected to the output. The program can change audio nodes dynamically so it should be possible to remove whiskers again if there is no longer anything connected to the sound output.

var whisker = await paper.get('whisker', {
  direction,      // "up" (default), "down", "left", "right"
  whiskerLength,  // as fraction of the side (default 0.7)
  requiredData,   // array of data fields that must be present in the other paper
  paperNumber,    // paper number to do this for (default is own paper number)
});

// destroy
whisker.destroy();

Different whisker color

If a paper has multiple whiskers it would be nice if you could change the color of the whisker to differentiate them better. Instead of a color change the "connected" state is indicated by the moving dot animation. If the paper is not connected a static line is rendered without the moving dot.

var whisker = await paper.get('whisker', {
  direction: "up",
  color: "blue", // new color attribute (default: red)
  requiredData: [ "frequency" ]
});

Separate event handlers for placement and removal of papers

I think it would be cleaner to have two separate events for when a paper is placed and when it's removed. This makes it easier to explain the API. It's not necessary to check for null and in the removed paper event handler we can also pass in the information about the paper which was removed.

whisker.on("paperAdded" ({paperNumber, paperObj}) => { /* do something */ })
whisker.on("paperRemoved", ({paperNumber, paperObj}) => { /* do something */})

- allow to create multiple whiskers
- whiskers can be removed
- event listeners can be attached to whiskers
  - paperRemoved
  - paperAdded
  - whiskerMoved
@@ -119,6 +136,9 @@ import { fillQuadTex, fillTriTex } from './canvasUtils';
callback,
whiskerPointCallback,
} = {}) => {
// eslint-disable-next-line no-console
console.warn('paper.whenPointsAt is deprecated, use the new whisker api instead');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how we should handle deprecation of features. I've kept the original whiskers for now, but long term I think we should remove paper.whenPointsAt to avoid confusion with the new whisker api.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove the code below and call get('whisker') when calling this? So we don't have any duplicate code at least?

The console.warn makes a lot of sense, let's keep that.

ctx.lineTo(whiskerEnd.x, whiskerEnd.y);
ctx.stroke();

const dotFraction = Math.abs(Math.sin(Date.now() / 600));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the dot animation to alternate direction. I wanted to avoid implying a direction of the dataflow which depends on what the whisker does and instead just visualize connectedness.

@@ -0,0 +1,171 @@
import EventEmitter from 'events';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've used the node EventEmitter because the browser event system adds unnecessary complication like event bubbling and dealing with Event objects instead of plain JS objects.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, on the other hand this does add a non-standard API into Paper Programs. I think I would slightly prefer to use EventTarget anyway because it's an accepted browser standard. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see your point. I would like to make Paper Programs more accessible to people who are new to programming and don't know the web platforms. In this case, I don't see a lot of benefits or more powerful things you could do with the native browser events other than being compliant with the standard.

Maybe we should also have a more general discussion what the future goals of Paper Programs are. In the future, I would like to explore different programming models to make it easier to build things with Paper Programs. I probably should write down my thoughts about this sometime.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that makes sense too. I'm mostly concerned about future compatibility, and bringing in concepts from Node feels a bit heavy in that respect. I don't have a super strong opinion though, so also happy to keep it this way.

And yeah, that would be useful! I don't really have goals for Paper Programs other than where you and other contributors want to take it. :)

docs/api.md Outdated
whisker.destroy()
```

*deprecated* old api:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just delete this entirely?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you referring to the part below "deprecated old api:" ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah!

}

// render whisker with dot animation
ctx.clearRect(0, 0, ctx.width, ctx.height);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this wipe out the other whiskers too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch this does nothing, because ctx has no width and height

@@ -0,0 +1,171 @@
import EventEmitter from 'events';

export default class WhiskerFactory {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what the point is of this factory? The code might be simpler by just giving each whisker a unique identifier and getting a unique canvas for each whisker. Then you can keep everything contained in class Whisker. Doesn't matter all that much, just thinking it might be simpler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to have just a single interval, but I agree that it would be simpler for each whisker to create its own interval. I'll change that.

Copy link
Owner

@janpaul123 janpaul123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some small feedback here and there. Feel free to push back on points. 😄 Thanks for the PR!!

@janpaul123
Copy link
Owner

Let me know when you're happy with the PR, then I'll merge it. 👍

@paulsonnentag
Copy link
Contributor Author

Ready to merge

@janpaul123
Copy link
Owner

Sorry for the late response 😞; and now it looks like there's a merge conflict. Let me know when it's resolved and I'll merge!

@paulsonnentag
Copy link
Contributor Author

I've resolved the merge conflicts now

Copy link
Owner

@janpaul123 janpaul123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome thanks!!

@janpaul123 janpaul123 merged commit 55bd3eb into janpaul123:master Jun 15, 2018
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

Successfully merging this pull request may close these issues.

2 participants