Javascript,绘制字符串,画布
Javascript, drawing a tringle, canvas
我也问过类似的问题,一些善良的灵魂试图提供帮助,但不幸失败了。
老师给我布置了一项作业;我要从html表单中收集数据,然后使用给定的坐标打印字符串。不幸的是,我的代码不起作用,我不知道错误在哪里。有什么想法吗?顺便说一句,这是我的第一个JS程序,所以请原谅我的不完美。
这是代码:https://jsfiddle.net/n07engyt/4/
function draw()
{
var welcome_parra = document.getElementById('form');
var coordinate = document.getElementById('wierzcholekX1')
var canvas = document.getElementById("canvas1");
var ctx = canvas.getContext("2d");
ctx.beginPath();
//ctx.moveTo(100,50);
ctx.moveTo(testX1(), testY1());
//ctx.lineTo(130, 100);
ctx.lineTo(testX2(), testY2());
ctx.lineTo(testX3(), testY3());
//ctx.lineTo(70, 100);
ctx.fillStyle = "rgba(0,0,0,1)";
ctx.fill();
}
function testX1()
{
var welcome_parra = document.getElementById('form');
var coordinate = document.getElementById('wX1')
return coordinate.value;
}
function testX2()
{
var welcome_parra = document.getElementById('form');
var coordinate = document.getElementById('wX2')
return coordinate.value;
}
function testX3())
{
var welcome_parra = document.getElementById('form');
var coordinate = document.getElementById('wX3')
return coordinate.value;
}
function testY1()
{
var welcome_parra = document.getElementById('form');
var coordinate = document.getElementById('wY1')
return coordinate.value;
}
function testY2()
{
var welcome_parra = document.getElementById('form');
var coordinate = document.getElementById('wY2')
return coordinate.value;
}
function testY3()
{
var welcome_parra = document.getElementById('form');
var coordinate = document.getElementById('wY3')
return coordinate.value;
}
function write_coordinate(){
draw();
}
我还有一个问题;有更好的方法吗?喜欢更优雅的不是通过对每个顶点使用分离函数?
使用一个函数来获取对象中的所有点会更好。如果你使用表单,你不必写document.getElement..
等。这里有一个例子,我相信这将是一个巧妙的解决方案
function getCoordinates() {
return{
x1:Number(cordinates.x1.value),
y1:Number(cordinates.y1.value),
x2:Number(cordinates.x2.value),
y2:Number(cordinates.y2.value),
x3:Number(cordinates.x3.value),
y3:Number(cordinates.y3.value)
}
}
function draw() {
var canvas = document.getElementById('canvas');
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
var p = getCoordinates();
ctx.fillStyle="#A2322E";
console.log(p)
ctx.beginPath();
ctx.moveTo(p["x1"],p["y1"]);
ctx.lineTo(p["x2"],p["y2"]);
ctx.lineTo(p["x3"],p["y3"]);
ctx.closePath();
ctx.fill();
}
}
#canvas{
border:2px solid #666;
}
<form name="cordinates">
<div>
<input name="x1" type=text placeholder="x1" value="70">
<input name="y1" type=text placeholder="y1" value="50">
</div>
<div>
<input name="x2" type=text placeholder="x2" value="100">
<input name="y2" type=text placeholder="y2" value="75">
</div>
<div>
<input name="x3" type=text placeholder="x3" value="100">
<input name="y3" type=text placeholder="y3" value="25">
</div>
</form>
<button onclick="draw()">draw</button><br/>
<canvas id="canvas" width="400" height="400"></canvas>
查看控制台,您将看到问题。。。
提示:额外的)
试试这个:
function draw()
{
var canvas = document.getElementById("canvas1");
var ctx = canvas.getContext("2d");
ctx.beginPath();
//ctx.moveTo(100,50);
ctx.moveTo(getElementValue('wierzcholekX1'), getElementValue('wierzcholekY1'));
//ctx.lineTo(130, 100);
ctx.lineTo(getElementValue('wierzcholekX2'), getElementValue('wierzcholekY2'));
ctx.lineTo(getElementValue('wierzcholekX3'), getElementValue('wierzcholekY3'));
//ctx.lineTo(70, 100);
ctx.fillStyle = "rgba(0,0,0,1)";
ctx.fill();
}
function getElementValue(inputID)
{
return document.getElementById(inputID).value;
}
function write_coordinate()
{
draw();
}
JSFiddle链接
相关文章:
- 如何在JavaScript中将字符串转换为函数引用
- Canvas Html5绘图应用程序,移动画布会导致重大问题
- jQuery中是否内置了任何字符串格式化函数
- 画布数据到图像
- Javascript,有没有一种方法可以将数组写成没有逗号或空格的单个文本字符串
- 使用键盘箭头在画布上移动字符串
- Javascript,绘制字符串,画布
- 根据64字符串绘制画布图片时出错
- 在画布上显示字符串仅显示几分之一秒
- 将画布上下文插入到动态创建的新窗口 html 中的 html 字符串
- 我无法从画布获取 Base64 字符串
- 新行无法显示在画布中的字符串中
- 确定 HTML5 画布中字符串的宽度
- 将画布中输入的文本检索为字符串
- 一个画布,我想加载一个Base64字符串,表示png图像,但只是显示一个空白的白色框
- JS问题时,试图将字符串转换为浮动在画布上
- 画布toDataURL从字符串
- 在服务器端保存画布图像时,从base64数据字符串生成空白图像
- 正在处理中按字符串将图像加载到画布
- 将svg转换为画布并获得base64字符串(画布)