admin管理员组

文章数量:1315984

I have a page that you will be able to draw squares on it. The squares will represent a room, so you have one square, then you make another over the first square and then two will join

It will looks like this .png

And functions like so /

I need to figure out how get them squares to make another shape that will look like the image on the right of the picture above.

If this helps, the square info is stored in points in this format

[
    {
        start: {x: 312, y: 164}, 
        end: {x: 734, y: 371}
    },
    {
        start: {x: 696, y: 304}, 
        end: {x: 1060, y: 561}
    }
]

Any links to resources that will help me do this will be of much help thank you

I have a page that you will be able to draw squares on it. The squares will represent a room, so you have one square, then you make another over the first square and then two will join

It will looks like this http://puu.sh/bllo7/95e2112d79.png

And functions like so http://jsfiddle/bLenxexL/

I need to figure out how get them squares to make another shape that will look like the image on the right of the picture above.

If this helps, the square info is stored in points in this format

[
    {
        start: {x: 312, y: 164}, 
        end: {x: 734, y: 371}
    },
    {
        start: {x: 696, y: 304}, 
        end: {x: 1060, y: 561}
    }
]

Any links to resources that will help me do this will be of much help thank you

Share Improve this question asked Sep 4, 2014 at 16:20 BenBen 5,7779 gold badges36 silver badges50 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

You can use positing to create a single stroke around your bined rectangles.

  • draw all your rectangles with strokes

  • set positing to 'destination-out'. This causes new drawings to "erase" existing drawings where the two overlap.

  • fill all your rects. This erases all but the outline of the bined rectangles.

This works because strokes are drawn half-inside & half-outside the border of rectangles. When you erase the inside of the bined rectangles you are left with the half-outside strokes.

Example code and a Demo: http://jsfiddle/m1erickson/m5jg92wd/

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery./jquery.min.js"></script>
<style>
    body{ background-color: ivory; }
    canvas{border:1px solid red;}
</style>
<script>
$(function(){

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

    // draw all rects with strokes
    ctx.strokeRect(20,20,100,200);
    ctx.strokeRect(90,110,75,50);

    // set positing to erase existing drawings 
    // new drawings will erase existing drawings where the two overlap
    ctx.globalCompositeOperation='destination-out';

    // fill all rects
    // This "erases" all but the outline stroke
    ctx.fillRect(20,20,100,200);
    ctx.fillRect(90,110,75,50);

    // reset positing to its default mode
    ctx.globalCompositeOperation='source-over';

}); // end $(function(){});
</script>
</head>
<body>
    <canvas id="canvas" width=300 height=300></canvas>
</body>
</html>

本文标签: javascriptHTML5 Canvas merging to rectangles to form a new shapeStack Overflow