Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource


Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource


Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.


                <canvas id="canvas" width="300" height="400" />


                body {
  padding: 20px;
  background: #eee;
#canvas {
  border: 1px solid #ccc;


                function findBreakPoint (text, width, context) {
    var min = 0;
    var max = text.length - 1;
    while (min <= max) {
        var middle = Math.floor((min + max) / 2);
        var middleWidth = context.measureText(text.substr(0, middle)).width;
        var oneCharWiderThanMiddleWidth = context.measureText(text.substr(0, middle + 1)).width;
        if (middleWidth <= width && oneCharWiderThanMiddleWidth > width) {
            return middle;
        if (middleWidth < width) {
            min = middle + 1;
        } else {
            max = middle - 1;
    return -1;
function breakLinesForCanvas (text, width, font) {
    var canvas = document.getElementById('canvas');
    var context = canvas.getContext('2d');
    var result = [];
    var breakPoint = 0;
    if (font) {
        context.font = font;
    while ((breakPoint = findBreakPoint(text, width, context)) !== -1) {
        result.push(text.substr(0, breakPoint));
        text = text.substr(breakPoint);
    if (text) {
    return result;

var result = breakLinesForCanvas('使用很寻常的二分查找,如果某一个位置之前的文字宽度小于等于设定的宽度,并且它之后一个字之前的文字宽度大于设定的宽度,那么这个位置就是文本的换行点。上面只是找到一个换行点,对于输入的一段文本,需要循环查找,直到不存在这样的换行点为止, 完整的代码如下', 300, '16px 微软雅黑');

var lineHeight = 30;
var context = document.getElementById('canvas').getContext('2d');
context.font = "16px 微软雅黑";

result.forEach(function (line, index) {
  context.fillText(line, 0, lineHeight * index + 30);