用单选按钮绘图

时间:2014-08-15 09:40:23

标签: javascript jquery

我是Javascript的初学者。我试图用一些单选按钮绘制形状。但是,按钮不起作用。当我绘制每个时,它都有效。我需要做什么?

<h2>Painting Editor</h2>

<br />

<div align="center">
<label>Line <input type="radio" name="Shape" value="Line" id="Line" checked /> </label>
<label>Rectangle <input type="radio" name="Shape" value="Rectangle" id="Rectangle" /> </label>
<label>Circle <input type="radio" name="Shape" value="Circle" id="Circle" /> </label>

上面的部分是分配单选按钮。

<br />

<div id="sketch">
<canvas id="paint" width="700" height="700"></canvas>
</div>

<script>
    (function () {
    var canvas = document.querySelector('#paint');
    var context = canvas.getContext('2d');

    var sketch = document.querySelector('#sketch');
    var sketch_style = getComputedStyle(sketch);

    // Pase into integer of Canvas size
    canvas.width = parseInt(sketch_style.getPropertyValue('width'));
    canvas.height = parseInt(sketch_style.getPropertyValue('height'));


    // Creating a tmp canvas
    var tmp_canvas = document.createElement('canvas');
    var tmp_context = tmp_canvas.getContext('2d');
    tmp_canvas.id = 'tmp_canvas';
    tmp_canvas.width = canvas.width;
    tmp_canvas.height = canvas.height;

    sketch.appendChild(tmp_canvas);

    // Mouse Position Setting
    var mouse = { x: 0, y: 0 };
    var last_mouse = { x: 0, y: 0 };

    /* Drawing on Paint*/
    tmp_context.lineWidth = 4;
    tmp_context.lineJoin = 'round';
    tmp_context.lineCap = 'round';
    tmp_context.strokeStyle = 'Red';
    tmp_context.fillStyle = 'Red';


    var shape = '';

    document.querySelector('#Line').onchange() = function () {
        if (this.checked)
            shape = 'Line';

        tmp_canvas.style.display = 'block';
    }

    /* Mouse Capturing Work */
    tmp_canvas.addEventListener('mousemove', function (e) {
        mouse.x = typeof e.offsetX === 'undefined' ? e.offsetX : e.layerX;
        mouse.y = typeof e.offsetY === 'undefined' ? e.offsetY : e.layerY;
    });

    tmp_canvas.addEventListener('mousedown', function (e) {
        tmp_canvas.addEventListener('mousemove', onLine);

        mouse.x = typeof e.offsetX === 'undefined' ? e.offsetX : e.layerX;
        mouse.y = typeof e.offsetY === 'undefined' ? e.offsetY : e.layerY;

        last_mouse.x = mouse.x;
        last_mouse.y = mouse.y;

        onLine();
    });

    tmp_canvas.addEventListener('mouseup', function () {
        tmp_canvas.removeEventListener('mousemove', onLine);

        // Writing down to real canvas now
        context.drawImage(tmp_canvas, 0, 0);

        // Clearing tmp canvas
        tmp_context.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height);
    });

    var onLine = function () {

        // Tmp canvas is always cleared up before drawing.
        tmp_context.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height);

        // Line
        tmp_context.beginPath();
        tmp_context.moveTo(last_mouse.x, last_mouse.y);
        tmp_context.lineTo(mouse.x, mouse.y);
        tmp_context.stroke();
        tmp_context.closePath();
    };

    document.querySelector('#Rectangle').onchange() = function () {
        if (this.checked)
            shape = 'Rectangle';

        tmp_canvas.style.display = 'block';
    }

    /* Mouse Capturing Work */
    tmp_canvas.addEventListener('mousemove', function (e) {
        mouse.x = typeof e.offsetX === 'undefined' ? e.offsetX : e.layerX;
        mouse.y = typeof e.offsetY === 'undefined' ? e.offsetY : e.layerY;
    });

    tmp_canvas.addEventListener('mousedown', function (e) {
        tmp_canvas.addEventListener('mousemove', onRect);

        mouse.x = typeof e.offsetX === 'undefined' ? e.offsetX : e.layerX;
        mouse.y = typeof e.offsetY === 'undefined' ? e.offsetY : e.layerY;

        last_mouse.x = mouse.x;
        last_mouse.y = mouse.y;

        onRect();
    });

    tmp_canvas.addEventListener('mouseup', function () {
        tmp_canvas.removeEventListener('mousemove', onRect);

        // Writing down to real canvas now
        context.drawImage(tmp_canvas, 0, 0);

        // Clearing tmp canvas
        tmp_context.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height);
    });

    var onRect = function () {
        // Rectangle
        var x = Math.min(mouse.x, last_mouse.x);
        var y = Math.min(mouse.y, last_mouse.y);
        var width = Math.abs(mouse.x - last_mouse.x);
        var height = Math.abs(mouse.y - last_mouse.y);
        tmp_context.strokeRect(x, y, width, height);
    };


    document.querySelector('#Circle').onchange() = function () {
        if (this.checked)
            shape = 'Circle';

        tmp_canvas.style.display = 'block';
    }

    /* Mouse Capturing Work */
    tmp_canvas.addEventListener('mousemove', function (e) {
        mouse.x = typeof e.offsetX === 'undefined' ? e.offsetX : e.layerX;
        mouse.y = typeof e.offsetY === 'undefined' ? e.offsetY : e.layerY;
    });

    tmp_canvas.addEventListener('mousedown', function (e) {
        tmp_canvas.addEventListener('mousemove', onCircle);

        mouse.x = typeof e.offsetX === 'undefined' ? e.offsetX : e.layerX;
        mouse.y = typeof e.offsetY === 'undefined' ? e.offsetY : e.layerY;

        last_mouse.x = mouse.x;
        last_mouse.y = mouse.y;

        onCircle();
    });

    tmp_canvas.addEventListener('mouseup', function () {
        tmp_canvas.removeEventListener('mousemove', onCircle);

        // Writing down to real canvas now
        context.drawImage(tmp_canvas, 0, 0);

        // Clearing tmp canvas
        tmp_context.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height);
    });

    var onCircle = function () {
        // Circle
        var x = (mouse.x + last_mouse.x) / 2;
        var y = (mouse.y + last_mouse.y) / 2;

        var radius = Math.max(Math.abs(mouse.x - last_mouse.x), Math.abs(mouse.y - last_mouse.y)) / 2;

        var sAngle = 0;
        var eAngle = 2 * Math.PI;

        tmp_context.beginPath();
        tmp_context.arc(x, y, radius, sAngle, eAngle);
        tmp_context.stroke();
        tmp_context.closePath();
    };
}());
</script>

创建了临时画布,我画了每个圆圈,矩形和直线。如果我单独制作,那就像我说的那样工作。我想将它们与单选按钮结合起来,但它没有用。

实施例。如果单击行单选按钮,则仅绘制线条。等...

1 个答案:

答案 0 :(得分:0)

在这些行onchange之后你有额外的括号

document.querySelector('#Line').onchange() = function () {

document.querySelector('#Rectangle').onchange() = function () {

document.querySelector('#Circle').onchange() = function () {

应该是

document.querySelector('#Line').onchange = function () {

如果没有括号:http://jsfiddle.net/rrb1xxjs/

,此测试似乎效果更好

您可以在mouseupmousedown上绑定一组函数,调用一个检查shape值的函数并调用所请求的绘图函数:

function draw() {
    // Tmp canvas is always cleared up before drawing.
    tmp_context.clearRect(0, 0, tmp_canvas.width, tmp_canvas.height);

    switch(shape) {
        case 'Line': onLine(); break;
        case 'Rectangle': onRect(); break;
        case 'Circle': onCircle(); break;
    }
}

此外,由于默认情况下选择了线路单选按钮,因此应该相应地初始化shape变量

var shape = 'Line';

Example here

这可能不是最好的方法,但我认为它与您现有的代码最匹配。