Drawing branches on a retina compatible canvas

Developer
Size
3,777 Kb
Views
24,288

How do I make an drawing branches on a retina compatible canvas?

Draws branches based on a random number of source / destination. Retina and hi-dpi compatible with canvas upscaling. What is a drawing branches on a retina compatible canvas? How do you make a drawing branches on a retina compatible canvas? This script and codes were developed by Daformat on 10 September 2022, Saturday.

Drawing branches on a retina compatible canvas Previews

Drawing branches on a retina compatible canvas - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Drawing branches on a retina compatible canvas</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <!-- Drawing branches with canvas ---------------------------- By @daformat Arrow head code found on: http://stackoverflow.com/questions/6576827/html-canvas-draw-curved-arrows To make it work on a Bezier curve, you need to provide the last control point's coordinate as the two first params of the "findAngle" function. The two last params should be the coordinates of the end point
-->
<div id="container"> <h2>Drawing branches with canvas</h2> <p>by <a href="https://twitter.com/daformat" target="_blank">@daformat</a></p> <p>Random number of branches on each side</p> <p>Retina compatible with canvas upscaling</p> <div> <canvas id="canvas" width="600" height="300"></canvas> </div> <aside> <h4>Max</h4> <p> <input id="max" type="range" value="5" min=1 max=15></input> <span id="maxValue"></span> </p> </aside> <p class="actual">Actual params: <span id="counter"></span></p>
</div> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Drawing branches on a retina compatible canvas - Script Codes CSS Codes

html{ width: 100%; height: 100%;
}
body{ font-family: Helvetica, Arial, sans-serif; background: #eee; color: #444; display: table; position: absolute; top:0; left:0; bottom: 0; right: 0; width: 100%; height: 100%
}
h2{ margin-top: 50px; color: #555; font-weight: 100; margin-bottom: 15px
}
p{ margin: 0; margin-bottom:10px
}
#container{ display: table-cell; width:100%; text-align: center; vertical-align:middle}
#canvas{ display: inline-block; margin-top: 40px; margin-bottom: 40px; box-shadow: 1px 1px 4px rgba(0,0,0,0.35); background:#f2f2f2; border: 4px solid white;
}
aside{ font-size: 14px; line-height: 1; color: #eee; display: inline-block; padding: 7px 30px; background: rgba(0,0,0,0.8); border-radius: 80px; border: 2px solid white; box-shadow: 1px 1px 4px rgba(0,0,0,0.5); width: 340px; margin-bottom: 20px
}
h4{ margin: 5px
}
aside p{ margin: 5px 0 10px 0;
}
aside input{ margin: 5px; width: 300px;
}
.actual{ font-size: 12px;
}

Drawing branches on a retina compatible canvas - Script Codes JS Codes

// returns radians
function findAngle( sx, sy, ex, ey) { // make sx and sy at the zero point return Math.atan2((ey - sy) , (ex - sx));
}
function drawArrowhead(ctx, locx, locy, angle, sizex, sizey) { var hx = sizex / 2; var hy = sizey / 2; console.log(arguments); ctx.translate((locx ), (locy)); ctx.rotate(angle); ctx.translate(-hx,-hy); ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(0,1*sizey); ctx.lineTo(1*sizex,1*hy); ctx.closePath(); ctx.fill();
}
function drawLine(ctx, sx, sy, ex, ey){ var xPosDiffers = (sx !== ex), yPosDiffers = (sy !== ey); ctx.save(); if ( xPosDiffers || yPosDiffers ){ ctx.beginPath(); ctx.moveTo(sx,sy); ctx.lineTo(ex,ey); ctx.stroke(); ctx.closePath(); } ctx.restore();
}
function drawBranch(drawBase, drawHead, $canvas, sx, sy, ex, ey){ var xPosDiffers = (sx !== ex), yPosDiffers = (sy !== ey), ang, ctx = $canvas[0].getContext('2d'), scale = $canvas.data('scale'); ctx.save();
console.log('Scale', scale); if ( xPosDiffers && yPosDiffers ){ // both x and y coordinates differ, let's go with a bezier curve var w = (ex-sx)/2; ctx.beginPath(); ctx.moveTo(sx,sy); ctx.bezierCurveTo(sx+w,sy,ex-w,ey,ex,ey); ctx.stroke(); ctx.closePath(); ang = findAngle(ex-w, ey, ex, ey); if(drawHead){ drawArrowhead(ctx, ex, ey, ang, 12*scale.x, 12*scale.y); } } else if ( xPosDiffers && !yPosDiffers) { // only the x position differs, let's draw a straight line ctx.beginPath(); drawLine(ctx, sx, sy, ex, ey); ctx.closePath(); ang = findAngle(sx, sy, ex, ey); if(drawHead){ drawArrowhead(ctx, ex, ey, ang, 12*scale.x, 12*scale.y); } } ctx.restore(); if( drawBase && (xPosDiffers || yPosDiffers) ) { ctx.beginPath(); ctx.rect(sx-5*scale.x, sy-5*scale.y, 10*scale.x, 10*scale.y); ctx.fill(); ctx.closePath(); }
}
function prepareCanvasForRetina($canvas){ // @2x var c = $canvas[0]; c.width = c.width; c.style.width = c.width+'px'; c.style.height = c.height+'px'; var scale = {x: 2, y:2}; $canvas.data('scale', scale); c.width = c.width * scale.x; c.height = c.height *scale.y; c.getContext('2d').scale( scale.x, scale.y); return $canvas;
}
function drawRelations($canvas, s1, s2){ console.log('Draw relations', arguments); var c = $canvas[0], ctx = c.getContext('2d'), padding = 40*$canvas.data('scale').x, height = c.height-2*padding, width = c.width-2*padding, middle = { x: (c.width-2*padding)/2 + padding - 4, y: (c.height-2*padding)/2 + padding, }, average = Math.max(s1.length, s2.length), scale = $canvas.data('scale'); // clear the canvas first c.width = c.width; console.log(ctx, width, height); ctx.strokeStyle = '#666'; ctx.fillStyle = '#333'; ctx.lineWidth = 4 * scale.x; var i, yStep, xStep = middle.x; yStep = s1.length > 0 ? height / (s1.length-1) : 0; for ( i = 0; i < s1.length; i++) { drawBranch(true, false, $canvas, padding, (s1.length>1 ? padding + (i*yStep) : middle.y) , middle.x, middle.y); } yStep = s2.length > 0 ? height / (s2.length-1) : 0; for ( i = 0; i < s2.length; i++) { drawBranch(false, true, $canvas, middle.x, middle.y, width +padding, (s2.length>1 ? padding + (i*yStep) : middle.y) ); }
}
$(function(){ prepareCanvasForRetina($('#canvas')); var t =window.setInterval(function(){ var m = $('#max').val(); $('#maxValue').text(m) var n1 = Math.round(Math.random()*m), n2 = Math.round(Math.random()*m); $('#counter').html('<strong>'+n1+'</strong> left, <strong>'+n2+'</strong> right'); drawRelations($('#canvas'),new Array(n1),new Array(n2))}, 800) });
Drawing branches on a retina compatible canvas - Script Codes
Drawing branches on a retina compatible canvas - Script Codes
Home Page Home
Developer Daformat
Username daformat
Uploaded September 10, 2022
Rating 3
Size 3,777 Kb
Views 24,288
Do you need developer help for Drawing branches on a retina compatible canvas?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Daformat (daformat) Script Codes
Create amazing blog posts with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!