【问题标题】:Is there a nice way to add new box and redraw the canvas using javascript?有没有一种很好的方法来添加新框并使用 javascript 重绘画布?
【发布时间】:2016-06-15 16:22:22
【问题描述】:

底层代码在画布中创建随机块。 这是我迄今为止所取得的成就。但我在做进一步的任务时遇到了困难。我想在画布外制作一个按钮(这很容易),点击在画布中添加了一个新框。旧盒子的位置可能会或可能不会改变(它取决于提供适当的空间)。如果新盒子没有足够的空间,则增加内部容器的大小,这将增加画布的大小,并用新的画布比例重新绘制旧盒子。如果内部容器大小增加外部容器,滚动条将进入外部容器。 这是我当前的代码:

HTML:

<div class="outer-container">
    <div class="inner-container">
        <canvas id="canvas" style="height:100%;width:100%"></canvas>
    </div>
</div>

Javascript:

function getRandomColor() {
    var color = '#';
    for (var i = 0; i < 6; i++) {
        color += (Math.random() * 16 | 0).toString(16);
    }
    return color;
}

function Point(x, y) {
    this.x = x;
    this.y = y;
}

function Rectangle(p1, p2) {
    this.p1 = p1;
    this.p2 = p2;
}

Rectangle.prototype.isInside = function (r) {
    function check(a, b) {
        return (
            a.p1.x <= b.p1.x && b.p1.x <= a.p2.x && a.p1.y <= b.p1.y && b.p1.y <= a.p2.y ||
            a.p1.x <= b.p2.x && b.p2.x <= a.p2.x && a.p1.y <= b.p2.y && b.p2.y <= a.p2.y ||
            a.p1.x <= b.p2.x && b.p2.x <= a.p2.x && a.p1.y <= b.p1.y && b.p1.y <= a.p2.y ||
            a.p1.x <= b.p1.x && b.p1.x <= a.p2.x && a.p1.y <= b.p2.y && b.p2.y <= a.p2.y
        );
    }
    return check(this, r) || check(r, this);
}

function generateRectangles() {
    function p() { return Math.random() * 300 | 0; }
    function s() { return 50 + Math.random() * 150 | 0; }

    var rectangles = [],
        r, size, x, y, isInside, i, counter = 0;

    for (i = 0; i < 20; i++) {
        counter = 0;
        do {
            counter++;
            x = p();
            y = p();
            size = s();
            r = new Rectangle(new Point(x, y), new Point(x + size, y + size));
            isInside = rectangles.some(function (a) {
                return a.isInside(r);
            });
        } while (isInside && counter < 1000);
        counter < 1000 && rectangles.push(r);
    }
    return rectangles;
}

function drawRectangles(rectangles) {
    var canvas = document.getElementById("canvas"),
        ctx = canvas.getContext("2d");

    rectangles.forEach(function (a) {
        ctx.lineWidth = 1;
        ctx.strokeRect(a.p1.x + 0.5, a.p1.y + 0.5, a.p2.x - a.p1.x - 1, a.p2.y - a.p1.y - 1);
        ctx.fillStyle = getRandomColor();
        ctx.fillRect(a.p1.x + 0.5, a.p1.y + 0.5, a.p2.x - a.p1.x - 1, a.p2.y - a.p1.y - 1);
    });
}

var rectangles = generateRectangles();
drawRectangles(rectangles);

我只是不知道如何重绘它。任何帮助将不胜感激。

【问题讨论】:

  • 您当前的代码是否有效?此外,请了解您将始终需要自己跟踪并执行调整框大小的逻辑。画布只是将内容光栅化到屏幕上的一种方式。它不会为您调整内容大小;在这方面它实际上是单向的。是的,您可以从中获取像素,但是关于这些像素的所有上下文都将消失。
  • 什么意思:“旧盒子的位置可能会或可能不会改变(取决于提供适当的空间)”?您的drawRectangles 函数已经在rectangles[] 中绘制了每个矩形。所以你可以(1)点击测试你可能的新矩形,(2)rectangles.push你的new Rectangle...变成rectangles,(3)清除画布,(4)必要时调整容器和画布的大小(使用溢出滚动容器(如有必要),& (5) 致电drawRectangles。顺便说一句,不要使用 CSS 调整画布元素的大小——这会挤压和/或拉伸你的矩形。相反,调整画布元素本身的大小。
  • 在询问有关画布的问题并描述上述行为时,我建议始终添加屏幕截图或类似内容,这样其他人更容易快速了解想法。我还建议您在 codepen.io 或类似文件中放置您所拥有的工作示例。

标签: javascript css html canvas


【解决方案1】:

创建按钮,带有 onclick="draw();" 属性和 javascript draw() 函数来绘制您想要的任何内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-20
    • 2016-02-21
    • 2011-06-23
    • 1970-01-01
    • 2021-12-19
    • 2023-02-05
    相关资源
    最近更新 更多