簡體   English   中英

我有一個畫布,我想在上面生成隨機矩形。 我似乎無法正常工作

[英]I have a canvas that I'd like to generate random rectangles on. I can't seem to get it to work

<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>
<script>
window.onload {
function rectWidth() {
    var width = document.getElementById('Canvas').offsetWidth;
    Math.floor((Math.random() * 100) + 1);
}
function rectHeight() {
    var width = document.getElementById('Canvas').offsetHeight;
    Math.floor((Math.random() * 200) + 2);
}
function randColor() {
    var randomColor = Math.floor(Math.random()*16777215).toString(16);
}
return rectWidth;
return rectHeight;
return randColor;
$(#rect).html("<rect width="return rectWidth;" height="return rectHeight" style=""color="return randColor"/>")}
}
</script>
</head>
<body>
<canvas id="Canvas" width="900" height="200">
<rect id="rect" width="250" height="250" style="color=blue"/></canvas>

我只是想弄清楚如何更改矩形的寬度,高度和顏色。

位置將在下一位。

我將如何更改代碼以使其正常工作?

如何在畫布上繪制矩形的示例:

    var canvas = document.getElementById("Canvas");
    var ctx = canvas.getContext("2d");
    ctx.beginPath();
    var width = 10;
    var height = 10;
    var x = 0;
    var y = 0;
    ctx.fillStyle = "#FFF0000";
    ctx.rect(x, y, width, height);
    ctx.fill();
    ctx.stroke();

這是一個快速的小提琴,可以在畫布上繪制大小隨機,顏色隨機的矩形: https : //jsfiddle.net/6omtu0pg/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM