HTML5 Canvas Text Wrap Test
How do I make an html5 canvas text wrap test?
Simple app to help you figure out how text wrap can be implemented in your HTML5 Canvas applications. A base example that can be easily expanded on for your needs. What is a html5 canvas text wrap test? How do you make a html5 canvas text wrap test? This script and codes were developed by Ash Blue on 18 July 2022, Monday.
HTML5 Canvas Text Wrap Test - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>HTML5 Canvas Text Wrap Test</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <style> /* NOTE: The styles were added inline because Prefixfree needs access to your styles and they must be inlined if they are on local disk! */ body { margin: 10px;
}
form label { display: block;
}
form li { margin: 10px 0;
}
input, textarea { padding: 5px; border: 1px solid #ccc; width: 300px;
}
textarea { height: 100px;
}
canvas { border: 1px solid #ccc;
} </style> <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
</head>
<body> <h1>Canvas Text Wrap Test</h1>
<form> <ul> <li> <label for="width">Width (px)</label> <input id="width" type="number" value="200" max="500" required /> </li> <li> <label for="font-size">Font Size (px)</label> <input id="font-size" type="number" value="12" /> </li> <li> </li> <li> <label>Text</label> <textarea id="text" required>Seven hundred twelve counts of extortion. Eight hundred and forty-nine counts of racketeering. Two hundred and forty-six counts of fraud. Eighty-seven counts of conspiracy murder. Five hundred and twenty-seven counts of obstruction of justice. How do the defendants plead?</textarea> </li> <li> <input id="submit" type="submit"> </li> </ul>
</form>
<h2>Result</h2>
<canvas id="canvas" width="500" height="500"></canvas> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
HTML5 Canvas Text Wrap Test - Script Codes CSS Codes
body { margin: 10px;
}
form label { display: block;
}
form li { margin: 10px 0;
}
input, textarea { padding: 5px; border: 1px solid #ccc; width: 300px;
}
textarea { height: 100px;
}
canvas { border: 1px solid #ccc;
}
HTML5 Canvas Text Wrap Test - Script Codes JS Codes
var canvas = $('#canvas');
var ctx = canvas.get(0).getContext('2d');
$('#submit').click(function (e) { e.preventDefault(); var text = $('#text').val(), fontSize = parseInt($('#font-size').val()), width = parseInt($('#width').val()), lines = [], line = '', lineTest = '', words = text.split(' '), currentY = 0; ctx.font = fontSize + 'px Arial'; for (var i = 0, len = words.length; i < len; i++) { lineTest = line + words[i] + ' '; // Check total width of line or last word if (ctx.measureText(lineTest).width > width) { // Calculate the new height currentY = lines.length * fontSize + fontSize; // Record and reset the current line lines.push({ text: line, height: currentY }); line = words[i] + ' '; } else { line = lineTest; } } // Catch last line in-case something is left over if (line.length > 0) { currentY = lines.length * fontSize + fontSize; lines.push({ text: line.trim(), height: currentY }); } // Visually output text ctx.clearRect(0, 0, 500, 500); for (var i = 0, len = lines.length; i < len; i++) { ctx.fillText(lines[i].text, 0, lines[i].height); }
});
Developer | Ash Blue |
Username | ashblue |
Uploaded | July 18, 2022 |
Rating | 3 |
Size | 3,076 Kb |
Views | 46,552 |
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!
Name | Size |
HTML5 Editable Table | 3,252 Kb |
HTML5 Canvas Progress Bar | 2,827 Kb |
CSS Sprite Animation | 3,732 Kb |
A Pen by Ash Blue | 2,071 Kb |
Loader Example | 3,637 Kb |
CSS Arrows | 1,921 Kb |
Overflowing Gallery | 1,566 Kb |
JavaScript Anagram Algorithm Explanation | 1,897 Kb |
Flow Chart Prototype | 4,149 Kb |
CSS Zoom Icon | 1,553 Kb |
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!
Name | Username | Size |
Simple blog concept | Drew_mc | 2,666 Kb |
Fun animations with CSS3 | Minimalmonkey | 2,360 Kb |
A Pen by Malith Hettiarachchi | MalZiiirA | 5,531 Kb |
Automatic scroll | Skeurentjes | 4,042 Kb |
Marching Squares Visualized | Sakri | 7,074 Kb |
Svg animation draw | SzymonDziewonski | 5,545 Kb |
Sequence.js - Sliding Horizontal Parallax | Maheshc | 4,537 Kb |
Emberjs Bootstrap Modal Carousel | Somethingkindawierd | 4,233 Kb |
Wave Lines | Mikehobizal | 4,023 Kb |
ECharts Version 3.0 - Bar Marker Chart | WebCodePro | 2,726 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!