HTml js 生成圖片


<script type="text/javascript">
    function $(id) {
        return document.getElementById(id);
    }
    function textToImg() {
        var len = $('len').value || 30;
        var i = 0;
        var fontSize = $('fontSize').value || 15;
        var fontWeight = $('fontWeight').value || 'normal';
        var txt = $("txt").value;
        var canvas = $('canvas');
        if (txt == '') {
            alert('請輸入文字!');
            $("txt").focus();
        }
        if (len > txt.length) {
            len = txt.length;
        }
        canvas.width = fontSize * len;
        canvas.height = fontSize * (3 / 2)
                * (Math.ceil(txt.length / len) + txt.split('\n').length - 1);
        var context = canvas.getContext('2d');
        context.clearRect(0, 0, canvas.width, canvas.height);
        context.fillStyle = $("showcolor").innerHTML;
        context.font = fontWeight + ' ' + fontSize + 'px sans-serif';
        context.textBaseline = 'top';
        canvas.style.display = 'none';
        console.log(txt.length);
        function fillTxt(text) {
            while (text.length > len) {
                var txtLine = text.substring(0, len);
                text = text.substring(len);
                context.fillText(txtLine, 0, fontSize * (3 / 2) * i++,
                        canvas.width);
            }
            context.fillText(text, 0, fontSize * (3 / 2) * i, canvas.width);
        }
        var txtArray = txt.split('\n');
        for ( var j = 0; j < txtArray.length; j++) { fillTxt(txtArray[j]); context.fillText('\n', 0, fontSize * (3 / 2) * i++, canvas.width); } var imageData = context.getImageData(0, 0, canvas.width, canvas.height); var img = $("img"); img.src = canvas.toDataURL("image/png"); } function changeColor() { var c = $("text"); var ctx = c.getContext("2d"); var red = $("red"); var green = $("green"); var blue = $("blue"); ctx.fillStyle = "rgb(" + red.value + "," + green.value + "," + blue.value + ")"; $("showcolor").innerHTML = ctx.fillStyle; ctx.fillRect(0, 0, 100, 100); //$('canvas').getContext('2d').fillStyle=$("showcolor").innerHTML; } </script>
<div>
    <textarea id="txt" style="width:450px;height:400px;">如果您的長微博字數超過了140個字的限制,在這里輸入微博內容,點擊右方的“生成圖片”,鼠標右擊右邊“文字”,“圖片另存為...”,保存圖片后,就可以直接發到微博里了,趕緊試試吧! </textarea> 
</div>
<div style="width:200px;height:100px;float:left;clear:right;">
    <table>
        <tbody>
            <tr>
                <td>
                    <label>字體大小:</label><input size="4" id="fontSize" value="15" />px
                </td>
            </tr>
            <tr>
                <td>
                    <label>字體精細:</label><select id="fontWeight"> <option value="normal">正常</option>
            <option value="bold"></option>
        </select>
                </td>
            </tr>
            <tr>
                <td>
                    <label>每行顯示:</label><input size="4" id="len" value="40" />個字
                </td>
            </tr>
            <tr>
                <td>
                    <canvas id="text" width="100" height="100"></canvas>
                    <p>
                        Red: <input type="range" id="red" min="0" max="255" value="0" onchange="changeColor();" />
                    </p>
                    <p>
                        Green:<input type="range" id="green" min="0" max="255" value="0" onchange="changeColor();" />
                    </p>
                    <p>
                        Blue: <input type="range" id="blue" min="0" max="255" value="0" onchange="changeColor();" />
                    </p>
目前的顏色:<span id="showcolor"></span>
                </td>
            </tr>
            <tr>
                <td>
                    <button onclick="textToImg();;">生成圖片</button> 
                </td>
            </tr>
        </tbody>
    </table>
</div>
<canvas id="canvas" style="display:block;"></canvas>
<div>
    <img id="img" style="border:1px solid;" />
</div>
<script>
    changeColor();
</script>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM