Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

repeating #6

Open
simonkcleung opened this Issue May 20, 2012 · 5 comments

Comments

Projects
None yet
2 participants

I think there should be a function that repeats last called method.

An alternative to your roundRect function:

function roundRect(x, y, w, h, r) {
w+=x; h+=y;
return this.beginPath().moveTo(x + r, y)
.arcTo(w, y, w, h, r).re(w, h, x, h, r)
.re(x, h, x, y, r).re(x, y, w, y, r).closePath();
}

the "re" function is a build-in function always point to last method, here i.e. arcTo.

Should it be a feature that saves the chains ......and reuse it. It is achievable by Function.bind()?

Owner

LeaVerou commented May 20, 2012

I’m afraid that’s not technically possible in a robust way. To make functions chainable, chainvas just returns this. There is no wrapper (like jQuery), so it’s impossible to store that kind of information.

The only way I can think of is to store the function in a property of the Chainvas object, and have .re call that. However, that could lead in unpredictable results in cases where it's used on different contexts, like:

element.setAttribute('foo', 'bar');
canvasCtx.re(...); // Equivalent to canvasCtx.setAttribute(...), which makes no sense

That would also introduce some overhead, as every method call should store a reference to itself in that property, just so that re could use it.

Of course, it's entirely possible that there's a better solution I'm just not seeing.

If you focus on canvas, the "re" function is useful (for lazy persons).

I add a line in the chainable function:

chainable: function(method) {
return function() {
var ret = method.apply(this, arguments);
this.re=arguments.callee; // *
return ret === undefined? this : ret;

However, using callee is not allowed in strict mode.

And BTW, the roundRect using arcTo in Opera is not correctly shown.

Owner

LeaVerou commented May 31, 2012

Right, that cleverly solves one of the issues I mentioned, but not the other (overhead).

I see the problem that adding a "re" property to the object may not be useful, especially the DOM interfaces.

I have an idea actually I used. It is a mix of function and object. See http://bbs.51js.com/thread-90256-1-1.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment