chainvas icon indicating copy to clipboard operation
chainvas copied to clipboard

repeating

Open simonkcleung opened this issue 13 years ago • 5 comments

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.

simonkcleung avatar May 20 '12 04:05 simonkcleung

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

simonkcleung avatar May 20 '12 07:05 simonkcleung

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.

LeaVerou avatar May 20 '12 07:05 LeaVerou

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.

simonkcleung avatar May 31 '12 16:05 simonkcleung

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

LeaVerou avatar May 31 '12 21:05 LeaVerou

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

simonkcleung avatar Jun 16 '12 14:06 simonkcleung