[英]I have a canvas that I'd like to generate random rectangles on. I can't seem to get it to work
Code 码
<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>
I'm just trying to figure out how I can change the width, height, and color of the rectangle. 我只是想弄清楚如何更改矩形的宽度,高度和颜色。
Position will come next. 位置将在下一位。
How would I change my code to get it to work? 我将如何更改代码以使其正常工作?
Example of how to draw a rectangle on 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();
Here's a quick fiddle which draws randomly sized, randomly colored rectangles on the canvas: https://jsfiddle.net/6omtu0pg/ 这是一个快速的小提琴,可以在画布上绘制大小随机,颜色随机的矩形: https : //jsfiddle.net/6omtu0pg/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.