我發生了此錯誤https://i.gyazo.com/77b934223522404fcb437efdbeb3f3d0.mp4,我試圖修復它。但是我所做的一切都不起作用。HTML5畫布 - 更改顏色
現場顏色在改變顏色時並未改變。
我希望有人能幫助我:)
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
<html>
<head>
<title>
game.rabascm.nl</title>
<style>
* { font-family: verdana; font-size: 10pt; COLOR: gray; -webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none; }
b { font-weight: bold; }
canvas { border: 1px solid gray;cursor: crosshair;}
td { text-align: center; padding: 25;}
</style>
<script src="jscolor.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>
<center>
<br><br>
<span id="display">Nothing to display</span>
<br><br>
Color: <input class="jscolor" id="colormixer" value="000000"><br>
<table>
<canvas width="400" height="400" id="canvas" ></canvas>
</table>
<br><br>
</center>
<script>
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
$('#canvas').mousedown(function(e){
var mouseX = e.pageX - this.offsetLeft;
var mouseY = e.pageY - this.offsetTop;
paint = true;
addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop);
redraw();
});
$('#canvas').mousemove(function(e){
if(paint){
addClick(e.pageX - this.offsetLeft, e.pageY - this.offsetTop, true);
redraw();
}
});
$('#canvas').mouseup(function(e){
paint = false;
});
$('#canvas').mouseleave(function(e){
paint = false;
});
var clickX = new Array();
var clickY = new Array();
var clickDrag = new Array();
var color = new Array();
var paint;
function addClick(x, y, dragging) {
clickX.push(x);
clickY.push(y);
clickDrag.push(dragging);
color.push(getCurrentColor());
}
var points = new Array();
$('#canvas').mousemove(function(e) {
if (paint) {
var point = {
x: e.pageX - this.offsetLeft,
y: e.pageY - this.offsetTop,
color: getCurrentColor()
};
points.push(point);
redraw();
}
});
function getCurrentColor(){
return document.getElementById("colormixer").value;
}
function redraw(){
var color = document.getElementById("colormixer").value;
context.fillStyle = "#" + color;
for(var i=0; i < clickX.length; i++) {
context.beginPath();
if(clickDrag[i] && i){
context.moveTo(clickX[i-1], clickY[i-1]);
}else{
context.moveTo(clickX[i]-1, clickY[i]);
}
var mousePosx = clickX[i];
var mousePosy = clickY[i];
mousePosx = mousePosx.toString().slice(0, -1) * 10;
mousePosy = mousePosy.toString().slice(0, -1) * 10;
var mousePosx = mousePosx.toString().split(".")[0];
var mousePosy = mousePosy.toString().split(".")[0];
context.fillRect(mousePosx, mousePosy, 10, 10);
document.getElementById("display").innerHTML = "Cords: " + mousePosx + ":" + mousePosy;
}
}
var bg = new Image();
bg.onload = function() {
for (i = 0; i < 20; i++) {
for (y = 0; y < 20; y++) {
var top = y * 20;
var left = i * 20;
context.drawImage(bg, left, top, 20, 20);
}}}
bg.src = 'http://www.rabascm.nl/game/bg.png';
</script>
</body>
</html>
請在此處提供代碼,而非外部。 –
好的,謝謝!完成! – Rabascm
問題是你不保存繪製的顏色,只有座標。在'Redraw'中,你設置了一個'fillStyle',然後循環遍歷所有的座標集並且只應用** fillStyle **。 –