• canvas绘制的文字如何换行


    <html>
    
    <head>
        <title>canvas绘制的文字如何换行</title>
        <style type="text/css">
    
        </style>
    </head>
    
    <body>
        <canvas id="canvas" height="400" width="400"></canvas>
        <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
        <script>
        $(function() {
            var myAction = {};
    
            $.extend(myAction, {
                drawText: function(t, x, y, w) {
                    var c = document.getElementById("canvas");
                    var context = c.getContext("2d");
                    var chr = t.split("");
                    var temp = "";
                    var row = [];
    
                    context.font = "20px Arial";
                    context.fillStyle = "black";
                    context.textBaseline = "middle";
    
                    for (var a = 0; a < chr.length; a++) {
                        if (context.measureText(temp).width < w) {;
                        } else {
                            row.push(temp);
                            temp = "";
                        }
                        temp += chr[a];
                    }
    
                    row.push(temp);
    
                    for (var b = 0; b < row.length; b++) {
                        context.fillText(row[b], x, y + (b + 1) * 20);
                    }
                },
                initData: function() {
                    myAction.drawText("Hello, World!What a nice day.", 0, 0, 110);
                }
            })
    
            var init = function() {
                myAction.initData();
            }();
        });
        </script>
    </body>
    
    </html>
    
  • 相关阅读:
    Leetcode 532.数组中的K-diff数对
    Leetcode 529.扫雷游戏
    定增游戏(三)
    套利不归路(五)
    定增游戏(一)
    套利不归路(四)
    套利不归路(三)
    套利不归路(二)
    套利不归路(一)
    减持大逃亡(二)
  • 原文地址:https://www.cnblogs.com/xutongbao/p/9924822.html
Copyright © 2020-2023  润新知