帆布图纸大小修复需要

时间:2015-02-17 01:08:27

标签: javascript html5 canvas

当我改变尺寸时,在我的画布上

我有它,所以你可以从1 - 100但我有它所以它在5的上升,但是当你到1并再次上升它然后再到6然后再去到11等等。

这里是http://jsfiddle.net/daniel9000/tsaytohj/2/

这是我的java脚本

function processData(c1, c2) {
var cv1 = document.getElementById(c1).value;
var cv2 = document.getElementById(c2).value;
alert(cv1 + "\n" + cv2);
}

var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');

var radius = 10;
var dragging = false;

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

context.lineWidth = radius * 2;

var putPoint = function (e) {
if (dragging) {
var bounds = canvas.getBoundingClientRect();
var mouseX = e.clientX + bounds.left;
var mouseY = e.clientY - bounds.top;
var mouseX = e.clientX + bounds.left - 0;
context.lineTo(mouseX, mouseY)
context.strokeStyle = document.getElementById('color1').value;
context.stroke();
context.beginPath();
context.arc(mouseX, mouseY, radius, 0, Math.PI * 2);
context.fillStyle = document.getElementById('color1').value;
context.fill();
context.beginPath();
context.moveTo(mouseX, mouseY);
}
}

var engage = function (e) {
dragging = true;
putPoint(e);
}

var disengage = function () {
dragging = false;
context.beginPath();
}

canvas.addEventListener('mousedown', engage);
canvas.addEventListener('mousemove', putPoint);
canvas.addEventListener('mouseup', disengage);

var setRadius = function (newRadius) {
if (newRadius < minRad) newRadius = minRad;
else if (newRadius > maxRad) newRadius = maxRad;
radius = newRadius;
context.lineWidth = radius * 2;

radSpan.innerHTML = radius;
}

var minRad = 1,
maxRad = 100,
defaultRad = 20,
interval = 5,
radSpan = document.getElementById('radval'),
decRad = document.getElementById('decrad'),
incRad = document.getElementById('incrad');

decRad.addEventListener('click', function () {
setRadius(radius - interval);
});
incRad.addEventListener('click', function () {
setRadius(radius + interval);
});

setRadius(defaultRad);

var button = document.getElementById('btn-download');
button.addEventListener('click', function (e) {
var dataURL = canvas.toDataURL('image/png');
button.href = dataURL;
});

2 个答案:

答案 0 :(得分:1)

这非常简单。我建议做这样的事情:

decRad.addEventListener('click', function () {
    setRadius(radius - interval);
});
incRad.addEventListener('click', function () {
    setRadius(radius < interval ? interval : radius + interval);
});

答案 1 :(得分:0)

这是因为你设置了minRad = 1,它不是5的倍数序列,大概是为了避免半径为零。因此,当你向它添加5时,你会得到6而不是5.你有几种可能的解决方案:

  • 设置minRad = 0
  • 仅允许最小半径为5,即minRad = 5
  • 如果不是上述任何一项,那么您必须通过将radius = newRadius;替换为:

    来补偿使用不按顺序排列的最小值
    radius = newRadius;
    if( radius == 6) radius = 5;