gpt4 book ai didi

javascript - Canvas 在刷新时开始

转载 作者:行者123 更新时间:2023-11-28 20:25:37 26 4
gpt4 key购买 nike

我有这个 Canvas ,您可以在其中绘制内容,但它只能在刷新页面时起作用,有人对此有解决方案吗?我尝试了 window.onload 或使用 image.onload 甚至是 canvas.onload 都不起作用,总是需要刷新。

http://urieldevelop.com/draw/index.html这就是页面。提前致谢。

代码在这里。

var atags = document.getElementsByTagName("a");
var canvas = document.getElementById('canvas');
var contexto = canvas.getContext("2d");
var width = 10, color = "#000", tipoDeLinea = "round", dibujando = false, x , y, mouseX = new Array(), mouseY = new Array();

function widthChange( n ){
width = n;
}

function colorChange( col ){
color = col;
}

function linea( t ){
tipoDeLinea = t;
}

contexto.lineJoin = "bevel";

function punto ( e ) {
x = e.offsetX, y = e.offsetY;
dibujando = true;
contexto.beginPath();
contexto.moveTo(x, y);
}

function puntoArriba ( e ) {
dibujando = false;
}

function dibuja ( e ) {
x = e.offsetX, y = e.offsetY;
mouseX.push(x);
mouseY.push(y);
var MXlength = mouseX.length, MYlength = mouseY.length;
if(dibujando == true){
contexto.lineJoin = tipoDeLinea;
contexto.lineWidth = width;
contexto.strokeStyle = color;
contexto.moveTo(mouseX[MXlength - 2], mouseY[MYlength - 2]);
contexto.lineTo(mouseX[MXlength - 1],mouseY[MYlength - 1]);
contexto.closePath();
contexto.stroke();
contexto.fill();
if(mouseX.length > 2 && mouseY.length > 2){
mouseX.shift();mouseY.shift();
}
}
}

canvas.addEventListener('mousedown', punto, false);
canvas.addEventListener('mousemove', dibuja, false);
canvas.addEventListener('mouseup', puntoArriba, false);
canvas.addEventListener('mouseout', puntoArriba, false);

for (var i = 0; i < atags.length; i++) {
atags[i].addEventListener("click", previene, false);
}

function previene(e){
e.preventDefault();
}

最佳答案

您遇到跨浏览器问题

问题出在您的事件处理程序中:Firefox 使用 .layerX/.layerY 而不是 .offsetX/.offset

因此,使用此跨浏览器代码来获取鼠标 x/y:

    var x = e.offsetX==undefined?e.layerX:e.offsetX;
var y = e.offsetY==undefined?e.layerY:e.offsetY;

更好的是,使用 jQuery 可以解决许多跨浏览器问题。

这是代码和 fiddle :http://jsfiddle.net/m1erickson/WeRRC/

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title> -\ dibuja con canvas</title>
<style type="text/css">
*{margin: 0;padding: 0;border: 0;outline: 0;}
.contenedor{width: 1200px;margin: 0 auto;height: 800px;}
.contenedor #canvas{width: 1200px;height: 600px;margin: 10px auto;border: 1px solid black;}
.anchos a, span{float: left;}
span{display: block;margin: 5px;}
.ancho1{display: block;width: 10px;height: 10px;margin: 10px 10px;background-color: black;}
.ancho2{display: block;width: 15px;height: 15px;margin: 7px 10px;background-color: black;}
.ancho3{display: block;width: 20px;height: 20px;margin: 4px 10px;background-color: black;}
.ancho4{display: block;width: 25px;height: 25px;margin: 1px 10px;background-color: black;}
.colores a{display: block;width: 25px;height: 25px;float: left;margin: 0px 10px;}
.colores span{display: block;float: left;}
.color1{background-color: red;}
.color2{background-color: green;}
.color3{background-color: blue;}
.color4{background-color: black;}
.color5{background-color: grey;}
.tipodelinea a{display: block;margin: 5px;float: left;}
</style>
</head>
<body>
<div class="contenedor">
<canvas id="canvas" height="600" width="1200"></canvas>
<div class="linea">
<div class="anchos">
<span>Anchos</span>
<a href="#" class="ancho1" onclick="widthChange(5)"></a>
<a href="#" class="ancho2" onclick="widthChange(10)"></a>
<a href="#" class="ancho3" onclick="widthChange(20)"></a>
<a href="#" class="ancho4" onclick="widthChange(30)"></a>
</div>

<div class="colores">
<span>Colores</span>
<a href="#" class="color1" onclick="colorChange('#ff0000')"></a>
<a href="#" class="color2" onclick="colorChange('#00ff00')"></a>
<a href="#" class="color3" onclick="colorChange('#0000ff')"></a>
<a href="#" class="color4" onclick="colorChange('#000000')"></a>
<a href="#" class="color5" onclick="colorChange('#999999')"></a>
</div>

<div class="tipodelinea">
<span>Tipo de linea</span>
<a href="#" class="linea1" onclick="linea('round')">Redondeado</a>
<a href="#" class="linea2" onclick="linea('bevel')">Relieve</a>
<a href="#" class="linea3" onclick="linea('miter')">Puntiagudo</a>
</div>
</div>
</div>
<script type="text/javascript">
window.onload=function(){

var atags = document.getElementsByTagName("a");
var canvas = document.getElementById('canvas');
var contexto = canvas.getContext("2d");
var width = 10, color = "#000", tipoDeLinea = "round", dibujando = false, x , y, mouseX = new Array(), mouseY = new Array();

console.log(canvas);

function widthChange( n ){
width = n;
}

function colorChange( col ){
color = col;
}

function linea( t ){
tipoDeLinea = t;
}

contexto.lineJoin = "bevel";

function punto ( e ) {
x = e.offsetX==undefined?e.layerX:e.offsetX;
y = e.offsetY==undefined?e.layerY:e.offsetY;
dibujando = true;
contexto.beginPath();
contexto.moveTo(x, y);
}

function puntoArriba ( e ) {
dibujando = false;
}

function dibuja ( e ) {
x = e.offsetX==undefined?e.layerX:e.offsetX;
y = e.offsetY==undefined?e.layerY:e.offsetY;
mouseX.push(x);
mouseY.push(y);


var MXlength = mouseX.length, MYlength = mouseY.length;
if(dibujando == true){
contexto.lineJoin = tipoDeLinea;
contexto.lineWidth = width;
contexto.strokeStyle = color;
contexto.moveTo(mouseX[MXlength - 2], mouseY[MYlength - 2]);
contexto.lineTo(mouseX[MXlength - 1],mouseY[MYlength - 1]);
contexto.closePath();
contexto.stroke();
contexto.fill();
if(mouseX.length > 2 && mouseY.length > 2){
mouseX.shift();mouseY.shift();
}
}
}

canvas.addEventListener('mousedown', punto, false);
canvas.addEventListener('mousemove', dibuja, false);
canvas.addEventListener('mouseup', puntoArriba, false);
canvas.addEventListener('mouseout', puntoArriba, false);

console.log("listening");

for (var i = 0; i < atags.length; i++) {
atags[i].addEventListener("click", previene, false);
}

function previene(e){
e.preventDefault();
}

}
</script>
</body>

</html>

关于javascript - Canvas 在刷新时开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17480657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com