tags:

views:

116

answers:

3

Hi all you geniuses,

q1: is it possible to have an invisible rectangle?

q2: is it possible to call method upon method? See below.

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

ctx.fillStyle = "";
ctx.strokeStyle = "";
// as far FF 3.67 goes, no way
// the goal is to fill the rectangle with some text
ctx.fillRect(50,50,50,20).fillText("you rock",250,250);

Thanks.

A: 

Why would you want an invisible rectangle? I assume to use as the bounds for your fillText() call. The answer is no: once you call fillRect() the canvas doesn't have conception of a rectangle existing at a certain location. Think of the canvas a dumb collection of pixels, nothing more, with just some helper methods so you don't have to set pixels one at a time.

pr1001
well, i need an ability to move a block of text on a canvas to another location of the same canvas and there are multiple blocks of texts (say, 20). And it seems that it might be easier to move a rectangle within a canvas vs. a block of text, hence, the questions. pls see the following thread for the "bigger" goal of the question, thanks.http://stackoverflow.com/questions/3313918/how-to-use-mouse-event-instead-of-keycode-for-html5-canvas-and-jquery
Don Don
You can't move anything in canvas, only modify pixels. If you need to keep track of where things are you need to do that separately or use a library that does it for you.
pr1001
svg, svg, thanks.
Don Don
Yeah, SVG is definitely the way to go, especially if you're not doing anything too intense (canvas is definitely faster). I highly recommend Raphael as an SVG library.
pr1001
+2  A: 

q1: is it possible to have an invisible rectangle?

Indeed it is!

ctx.fillStyle = 'rgba(255,255,255,0)';
ctx.fillRect(50,50,50,20);

Though this also works, and is a bit more concise:

;

q2: is it possible to call method upon method?

Conceivably you could do something like this:

//Naive generic chainer function
//returns this even if the default
//value is significant!
function chain(obj) {
 function F() {}
 F.prototype = obj;
 var cobj = new F();

 for (var i in obj) {
   if (typeof obj[i] == 'function') {
     //Function Bind-ish
     cobj[i] = (function() {
       var method = i;
       return function() {
         this.constructor.prototype[method].apply(this, arguments);
         return this;
       };
     }());
   }
 }
 return cobj;
}


var chained = chain(ctx);   

chained.fillRect(0,0,200,200)
.fillRect(100,100,200,200)
.fillRect(400,400,100,100)
.fillText("I CAN HAS INVISDIBLE??", 250, 250);

But why bother when you can do this:

with (ctx) {
  fillRect(100,100,200,200);
  fillRect(300,300,100,100);
  fillStyle = 'green'; //I CAN SEE
  fillText("SCREW BEST PRACTICES");
}
MooGoo
All right, thanks. But i think my questions are not clear enough as to the purpose for them. Please see the following question I posted without satisfactory answers yet:http://stackoverflow.com/questions/3313918/how-to-use-mouse-event-instead-of-keycode-for-html5-canvas-and-jqueryIn a nut shell, I'd like to be able to put some text inside a rectangle for it seems a bit easier for maneuvering (sp?) than a block of text, the goal is to be able to move some text around inside the canvas.
Don Don
Translating between keyboard and mouse input would seem like a question not really related to canvas at all. As has already been stated here, you cannot really move anything on canvas. You can merely simulate movement by drawing something, clearing the canvas, then drawing the same thing in a slightly different position. Are you sure you shouldn't be just using the DOM and CSS for this?
MooGoo
Yeah, it seems that SVG is the way to go, thanks.
Don Don
+1  A: 

You seem to be trying to use the canvas element as SVG. Try using Raphaël instead. Your 'text objects' will then actually be objects and you can move them around by adjusting their attributes, you can also define a group and move them all in a single operation. You will also be able to attach onclick events to them.

robertc
Yep, SVG is the baby. tks.
Don Don