Anis Hikmat Abu-hmiad Anis Hikmat Abu-hmiad - 6 months ago 15
HTML Question

How to fill custom shape has been draw in Canvas?

Im try to drew custom shape, but since I use moveTo ..I cant be filled it, so That my question is there's any way can may be determined points on screen to fill shape? or to do that I most use or drew another real shape in the same block as layer ...

Look at my example here to drew a simple shape:
Custom shape before & after fill

to I can fill image with blue color Im drew a Fill rectangle, so That is is a true way?

Code for shape for before fill:

var canvas3 = document.getElementById('canvas3');
var c3 = canvas3.getContext('2d');
c3.fillStyle = 'green';
c3.beginPath();
c3.moveTo(10,30);
c3.lineTo(200,30);
c3.moveTo(10,80);
c3.lineTo(200,80);
c3.moveTo(10,30);
c3.lineTo(10,180);
c3.moveTo(200,30);
c3.lineTo(200,180);
//c3.closePath();
c3.fill();
c3.lineWidth = 5;
c3.strokeStyle = 'orange';
c3.stroke();


Code for shape after fill:

var canvas3 = document.getElementById('canvas3');
var c3 = canvas3.getContext('2d');
c3.fillStyle = 'blue';
c3.beginPath();
c3.moveTo(10,30);
c3.fillRect(10,30,190,60);
c3.moveTo(10,30);
c3.lineTo(10,180);
c3.moveTo(10,90);
c3.lineTo(200,90);
c3.moveTo(200,30);
c3.lineTo(200,180);
c3.moveTo(10,30);
c3.lineTo(200,30);
//c3.closePath();
c3.fill();
c3.lineWidth = 5;
c3.strokeStyle = 'orange';
c3.stroke();


and finally which is a best way to I can drew shapes like this?
Note: Im new on html5 canvas and I read from this online book.

K3N K3N
Answer

is there any way can may be determined points on screen to fill shape? or to do that I most use or drew another real shape in the same block as layer

Just draw a shape in the same place. Fill first then stroke afterwards. A little planning may be required with canvas as to in which order to draw things.

You can define objects to hold the geometrical data if you plan to redraw often or move them around. This will certainly simplify the objective later on.

which is a best way to I can drew shapes like this?

In my opinion this code can be drawn much simpler and in fewer lines of codes. There is no need to break up a shape in several parts as in that code if you can draw a shape using a simple method for it. In this case four lines can be replaced with one rectangle.

Knowing how these shapes are drawn internally also helps so we can take advantage of the path a rect() leaves, i.e. closing in upper-left corner so we can continue from there.

For example:

var c3 = c.getContext("2d");

// fill blue box first as it will be at the bottom
c3.rect(10, 30, 190, 50);  // x, y, width, height
c3.fillStyle = 'blue';
c3.fill();

// orange stroke
// we know rect() will close at upper-left corner so continue from there with left leg
c3.lineTo(10, 180);
c3.moveTo(200, 80);        // create second leg at right separately
c3.lineTo(200, 180); 

c3.strokeStyle = "orange";
c3.lineWidth = 5;
c3.lineJoin = c3.lineCap = "round";
c3.stroke();
<canvas id=c height=180></canvas>

An alternative approach would be to fill then build the line path:

var c3 = c.getContext("2d");

c3.fillStyle = 'blue';
c3.fillRect(10, 30, 190, 50); // fillRect does not add to path

// orange stroke
c3.moveTo(10, 180);           // create "housing" starting at bottom-left corner
c3.lineTo(10, 30);            // upper-left
c3.lineTo(200, 30);           // upper-right
c3.lineTo(200, 180);          // bottom-right

c3.moveTo(10, 80);            // add cross-bar
c3.lineTo(200, 80); 

c3.strokeStyle = "orange";
c3.lineWidth = 5;
c3.lineJoin = c3.lineCap = "round";
c3.stroke();
<canvas id=c height=180></canvas>

Comments