简体   繁体   English

HTML5画布-输入类型范围

[英]HTML5 Canvas - Input type Range

I'm new to HTML5 programming. 我是HTML5编程的新手。 I have this project I need to make for my university, however, my professor never mention the input type Range and I need it. 我有我的大学需要做的这个项目,但是,我的教授从未提及输入类型Range,所以我需要它。 I need to make a square out of triangles, and it needs to be from 1 to 30 squares per line. 我需要用三角形做成一个正方形,并且它需要从每行1到30个正方形。 Here's the code I have. 这是我的代码。

function canvasDrawsTriangle(corFundo,corLinha, x0,y0,x1,y1,x2,y2){
    pintor.fillStyle=corFundo;
    pintor.strokeStyle=corLinha;
    pintor.beginPath();
    pintor.moveTo(x0,y0);
    pintor.lineTo(x1,y1);
    pintor.lineTo(x2,y2);
    pintor.closePath();
    pintor.stroke();
    pintor.fill();

function drawSquare(x, y, size) {
var centerX = x + size / 2;
var centerY = y + size / 2;
var farX = x + size;
var farY = y + size;
canvasDrawsTriangle("#449779", "#449779", centerX, centerY, x, y, x, farY);
canvasDrawsTriangle("#E6B569", "#E6B569", centerX, centerY, x, y, farX, y);
canvasDrawsTriangle("#AA8D49", "#AA8D49", centerX, centerY, farX, y, farX, farY);
canvasDrawsTriangle("#013D55", "#013D55", centerX, centerY, x, farY, farX, farY);

Than to draw multiple squares per line i have this function: 比每条线绘制多个正方形我有这个功能:

function drawMultipleSquares(x, y, size, horizontalCount, verticalCount) {
    for (var i = 0; i < horizontalCount; i++) {
        for (var j = 0; j < verticalCount; j++) {
           drawSquare(x + i * size, y + j * size, size);

With this code I just call my function in the Script like so: 使用此代码,我可以像下面这样在脚本中调用函数:

drawMultipleSquares(0, 0, 500/30, 30, 30);

Making me change manually the size variable everytime I want more or less squares in my canvas. 每当我想要在画布中增加或减少正方形时,就可以手动更改大小变量。 I would like to insert a slider to controll that variable and I have no idea how to do it! 我想插入一个滑块来控制该变量,但我不知道该怎么做! Please help. 请帮忙。 NOTE: The canvas has to be 500x500 so if the size is set to 500 it makes one big square, if the size is 500/30 it makes 30 squares per line. 注意:画布必须为500x500,因此,如果将大小设置为500,则将使一个大正方形;如果大小为500/30,则将使每行30个正方形。

 var pintor = document.getElementById('mycanvas').getContext('2d'); function canvasDrawsTriangle(corFundo,corLinha, x0,y0,x1,y1,x2,y2){ pintor.fillStyle=corFundo; pintor.strokeStyle=corLinha; pintor.beginPath(); pintor.moveTo(x0,y0); pintor.lineTo(x1,y1); pintor.lineTo(x2,y2); pintor.closePath(); pintor.stroke(); pintor.fill(); } function drawSquare(x, y, size) { var centerX = x + size / 2; var centerY = y + size / 2; var farX = x + size; var farY = y + size; canvasDrawsTriangle("#449779", "#449779", centerX, centerY, x, y, x, farY); canvasDrawsTriangle("#E6B569", "#E6B569", centerX, centerY, x, y, farX, y); canvasDrawsTriangle("#AA8D49", "#AA8D49", centerX, centerY, farX, y, farX, farY); canvasDrawsTriangle("#013D55", "#013D55", centerX, centerY, x, farY, farX, farY); } function drawMultipleSquares(x, y, size, horizontalCount, verticalCount) { for (var i = 0; i < horizontalCount; i++) { for (var j = 0; j < verticalCount; j++) { drawSquare(x + i * size, y + j * size, size); } } } drawMultipleSquares(0, 0, 500/30, 30, 30); function change(elm) { var val = document.querySelector('#horizontal').value; drawMultipleSquares(0, 0, 500/val, val , val); document.querySelector('#range').innerHTML = val; } 
 <input type="range" onchange="change()" id="horizontal" /><span id="range"></span> <hr /> <canvas id="mycanvas" width="500" height="500"></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM