画布文本不在其他画布内

时间:2014-11-20 11:37:48

标签: javascript html5 canvas html5-canvas

DEMO

文字&图像未在主容器/画布中对齐

HTML:

<canvas id="ex1" width="580" height="400" style="border:1px solid #ccc;">
            HTML Canvas not supported
        </canvas>   
        <canvas id="textC"></canvas>
        <canvas id="taskLogo"></canvas>

JS:

// 1. window.onload - wait for the page to be fully loaded.
        window.onload = function(){
            drawExamples();
            //drawEx1();
            drawLogo();
        }

        function drawExamples(){
            //2. obtain a reference to the canvas element
            var canvas = document.getElementById("ex1");
            var context = canvas.getContext("2d");

            var textCanvas = document.getElementById("textC");
            var textContext = textCanvas.getContext("2d");

            var textMetrics = context.measureText(textContext);
            var width = textMetrics.width;

            console.log(width)

            //4. Draw Graphics
            context.fillStyle = "#900"; // background 
            context.fillRect(14,25, 550, 350); // size - x y width height

            // stroke
            context.strokeStyle = "#000";
            context.lineWidth = 1;
            context.strokeRect(14,25, 550, 350); // size - x y width height

            //clear rectangle
            //context.clearRect(455,80,110,100);

            // canvas path starts
            context.beginPath();
                context.moveTo(100,10);
                context.lineTo(150,50);
                context.lineTo(200,50);
                context.lineTo(100,10);
                context.fill(); // stroke
            context.closePath();
            // canvas path ends

            textContext.font = "normal 90px Verdana";
            textContext.strokeStyle = "#f00";
            textContext.strokeText ("Developer", 15, 65);

            textContext.font    = "normal 15px Verdana";
            textContext.fillStyle= "#f00";
            textContext.fillText ("Developer", 20, 90)

        }

        // Image 
        var img1 = null;
        function drawLogo(){
            img1 = new Image();
            img1.src = "http://taskdesigns.com/task/images/logo.png";

            img1.addEventListener('load', drawImage1);
        }
        function drawImage1(){
            var imgCanvas = document.getElementById("taskLogo");
            var imgContext = imgCanvas.getContext("2d");

            imgContext.drawImage(img1, 20, 10)
        }

0 个答案:

没有答案