JavaScript How to draw a line in canvas with a background image draw line canvas javascript,draw line canvas android,draw line

I'm trying to draw a line in canvas (which I can do), but I want to put a repeating pattern on the line using a background image (unless there is another way to put a repeating background image on a line in canvas?).

How can I draw a line with a background image?

I understand the concept of clipping, but that only seems to work with shapes... not with a stroke. Any ideas?

Here is a jsfiddle of what I was trying http://jsfiddle.net/Z9cd7/

    function (callback) {
        window.setTimeout(callback, 1000 / 60);
    };
})();

var radius = 50;
var x = 100;
var dx = 10;
var y = 100;
var dy = 10;
var delay = 10;
var img = new Image();
img.onload = function () {
    var canvas1 = document.getElementById("image");
    var ctxImg = canvas1.getContext("2d");
    ctxImg.drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height);

    /*
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.save();
    ctx.beginPath();
    ctx.arc(100, 100, radius, 0, 2 * Math.PI, false);
    ctx.clip();
    ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height);
    ctx.restore();
    */
    ctx.moveTo(0,0)
    ctx.lineTo(100,100)
    ctx.lineWidth = 10;
    ctx.stroke()

    ctx.clip();
    ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, canvas.width, canvas.height);
    ctx.restore();

    //animate();
}
img.src = "http://lh3.ggpht.com/_Z-i7eF_ACGI/TRxpFywLCxI/AAAAAAAAAD8/ACsxiuO_C1g/house%20vector.png";
Answer:1

[ Using an image to draw a line ]

You can use this utility function to determine any percentage point along a line segment:

    function getLineXYatPercent(startPt,endPt,percent) {
        var dx = endPt.x-startPt.x;
        var dy = endPt.y-startPt.y;
        var X = startPt.x + dx*percent;
        var Y = startPt.y + dy*percent;
        return( {x:X,y:Y} );
    }

After that it's just a bit-of-math to repeatedly position your image along each line segement:

    var dx=points[i].x-points[i-1].x;
    var dy=points[i].y-points[i-1].y;
    var length=Math.sqrt(dx*dx+dy*dy);
    var pctImage=imgWidth/length;
    for(var pct=pctImage/2;pct<1.00;pct+=pctImage){
         var pos=getLineXYatPercent(points[i-1],points[i],pct);
         ctx.drawImage(img,0,0,img.width,img.height,pos.x,pos.y,imgWidth,imgHeight);
    }   

Demo: http://jsfiddle.net/m1erickson/R278u/

enter image description here

Code:

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

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");
    var $canvas=$("#canvas");
    var canvasOffset=$canvas.offset();
    var offsetX=canvasOffset.left;
    var offsetY=canvasOffset.top;
    var scrollX=$canvas.scrollLeft();
    var scrollY=$canvas.scrollTop();
    var isDown=false;

    var points=[];
    points.push({x:0,y:0});
    points.push({x:100,y:125});
    points.push({x:200,y:75});
    points.push({x:300,y:100});

    imgWidth=20;
    imgHeight=20;

    var img=new Image();
    img.onload=function(){
        draw();
    }
    img.src="https://dl.dropboxusercontent.com/u/139992952/multple/house%20vector.png";

    function draw(){
        ctx.clearRect(0,0,canvas.width,canvas.height);
        ctx.save();
        ctx.beginPath();
        ctx.moveTo(points[0].x,points[0].y);
        for(var i=1;i<points.length;i++){
            var p=points[i];
            var dx=points[i].x-points[i-1].x;
            var dy=points[i].y-points[i-1].y;
            var length=Math.sqrt(dx*dx+dy*dy);
            var pctImage=imgWidth/length;
            for(var pct=pctImage/2;pct<1.00;pct+=pctImage){
                var pos=getLineXYatPercent(points[i-1],points[i],pct);
                ctx.drawImage(img,0,0,img.width,img.height,pos.x,pos.y,imgWidth,imgHeight);
            }   
        }
    }

    function getLineXYatPercent(startPt,endPt,percent) {
        var dx = endPt.x-startPt.x;
        var dy = endPt.y-startPt.y;
        var X = startPt.x + dx*percent;
        var Y = startPt.y + dy*percent;
        return( {x:X,y:Y} );
    }

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

Is there any way to use Bootstrap to create a modal that expands to the width of the viewport? I've seen lots of suggestions (most of which involve changing .modal-dialog { width:auto} to .modal-...

Is there any way to use Bootstrap to create a modal that expands to the width of the viewport? I've seen lots of suggestions (most of which involve changing .modal-dialog { width:auto} to .modal-...

  1. bootstrap modal width auto
  2. bootstrap modal width and height
  3. bootstrap modal width 100
  4. bootstrap modal width responsive
  5. bootstrap modal width change
  6. bootstrap modal width large
  7. bootstrap modal width class
  8. bootstrap modal width size
  9. bootstrap modal width css
  10. bootstrap modal width not working
  11. bootstrap modal width fit content
  12. bootstrap modal width decrease
  13. bootstrap modal width according to content
  14. bootstrap modal width scrollbar
  15. bootstrap modal width
  16. bootstrap modal width extra large
  17. bootstrap modal width percentage
  18. bootstrap modal width based on content
  19. bootstrap modal width auto resize
  20. bootstrap modal width to content

I have an ng-repeat and I'm trying to get a specific order. I see that I can order using a list of properties: ng-repeat="client in clients | orderBy: ['isOpen', 'lastAccessTime']" My question is, ...

I have an ng-repeat and I'm trying to get a specific order. I see that I can order using a list of properties: ng-repeat="client in clients | orderBy: ['isOpen', 'lastAccessTime']" My question is, ...

  1. angular sort list alphabetically
  2. angular sort list by property
  3. angular sort list of objects
  4. angular sort list by date
  5. angular sort list by field
  6. angular sort list
  7. angular sort list drag drop
  8. angular sort list by name
  9. angular sort list pipe
  10. angular sort list in html
  11. angular sort list of strings
  12. angular sort list by number
  13. angular sort list in controller
  14. angular list sort animation
  15. angular 6 sort list
  16. angular 7 sort list
  17. angular 2 sort list
  18. angular material sort list
  19. angular sort dropdown list
  20. angular 4 sort list

I have a container that holds three items. One is floated left, another is centered and the last is floated right. If I remove the center item and add it back the right most item gets pushed down and ...

I have a container that holds three items. One is floated left, another is centered and the last is floated right. If I remove the center item and add it back the right most item gets pushed down and ...

I have made a JS/HTML5 game which is controlled by keyboard, I like to make it embeddable like flash games, but when I embed it inside an iframe it only capture keyboard events when iframe is focused. ...

I have made a JS/HTML5 game which is controlled by keyboard, I like to make it embeddable like flash games, but when I embed it inside an iframe it only capture keyboard events when iframe is focused. ...

  1. iframe capture keyboard events
  2. iframe keyboard capture