【问题标题】:Split HTML Canvas into rows with JavaScript使用 JavaScript 将 HTML Canvas 拆分为行
【发布时间】:2018-07-25 21:58:53
【问题描述】:

我正在尝试使用 JavaScript 和 HTML Canvas 创建动画故障效果。我已经设法将我的图像绘制到画布上,并想编写一个单独的函数,稍后将这个图像分成几行,以便它们可以单独动画。

我调用我的函数来创建画布......

var logo_text = "logo_text.png";

drawOnCanvas(logo_text, mc_logo_text, 0, 0, 245, 60);

这是函数的样子......

function drawOnCanvas(image, divID, posX, posY, wid, hei) {

//////////// CREATE CANVAS /////////////

var canvas = document.createElement("CANVAS"),
    ctx = canvas.getContext("2d");
    canvas.width = wid;
    canvas.height = hei;
    canvas.id = divID.id+"_canvas";
    divID.appendChild(canvas);

//////////// DRAW IMAGE /////////////

var img = new Image();

    img.src = image;

    img.onload = function () {
        ctx.drawImage(img, posX, posY, wid, hei);
    } }

这个函数工作正常,可以在我需要的地方绘制图像。我只是想不出将它分成几行的最佳方法。

【问题讨论】:

    标签: javascript canvas split html5-canvas effect


    【解决方案1】:

    试试这个!

    您只需要编辑_config数组的每个对象中的设置。

    drawOnCanvas() 将与_config 中的对象编号相同。

    顺便说一句,脚本需要在DIVs下面导入,所以document.getElementById可以找到它们。

    <body>
        <div id="mc_logo_text1"></div>
        <div id="mc_logo_text2"></div>
        <div id="mc_logo_text3"></div>
    
        <script>
            // Simply edit all settings here
            var _config = [
                {
                    src: 'logo_text.png',
                    domDiv: 'mc_logo_text1',
                    position: {x: 0, y: 0},
                    size: {w: 245, h: 60}
                },
                {
                    src: 'logo_text.png',
                    domDiv: 'mc_logo_text2',
                    position: {x: 0, y: 0},
                    size: {w: 245, h: 60}
                },
                {
                    src: 'logo_text.png',
                    domDiv: 'mc_logo_text3',
                    position: {x: 0, y: 0},
                    size: {w: 245, h: 60}
                }
            ]
    
            /////// No Need to modify ////////
            var drawOnCanvas = function(config) {
    
            var canvas = document.createElement("CANVAS");
                var ctx = canvas.getContext("2d");
                //Define which DIV to include new Canvas
                var domTaget = document.getElementById(config.domDiv);
    
            // Create Canvas & give new ID name
            function _canvasInit() {    
                canvas.width = config.size.w;
                canvas.height = config.size.h;
    
                canvas.id = domTaget.id + "_canvas";
                domTaget.appendChild(canvas);
            }
    
            // draw images into canvas
            function _drawImgs() {
                var img = new Image();
                img.src = config.src;
    
                img.onload = function() {
                ctx.drawImage(img, config.position.x, config.position.y, canvas.width, canvas.height);
                }
            }
    
                    // Do all functions
            _canvasInit();
            _drawImgs();
            }
    
            // Each object in _config array do drawOnCanvas function,
            for (var i = 0; i < _config.length; i = i + 1) {
                drawOnCanvas(_config[i]);
            }
            /////// No Need to modify ////////
    
        </script>
    </body>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-11
      • 1970-01-01
      • 2014-12-29
      • 2021-05-27
      • 2015-12-30
      • 2021-09-24
      • 2022-08-21
      • 2020-05-17
      相关资源
      最近更新 更多