【问题标题】:Creating a canvas cursor, unable to store color创建画布光标,无法存储颜色
【发布时间】:2014-01-21 11:01:45
【问题描述】:

在尝试了许多不同的事情之后,我终于在输入画布时获得了一个光标来调整大小,但不知道如何保存颜色。

我的鼠标基于这个演示:http://jsfiddle.net/AbdiasSoftware/XcjX9/

function loop() {
    var color = 'rgb(' + ((255 * Math.random())|0) + ','
                       + ((255 * Math.random())|0) + ','
                       + ((255 * Math.random())|0) + ')';
    makeCursor(color);
    setTimeout(loop, 1000);
}

function makeCursor(color) {

    var cursor = document.createElement('canvas'),
        ctx = cursor.getContext('2d');

    cursor.width = 16;
    cursor.height = 16;

    ctx.strokeStyle = color;

    ctx.lineWidth = 4;
    ctx.lineCap = 'round';

    ctx.moveTo(2, 12);
    ctx.lineTo(2, 2);
    ctx.lineTo(12, 2);
    ctx.moveTo(2, 2);
    ctx.lineTo(30, 30)    
    ctx.stroke();

    document.body.style.cursor = 'url(' + cursor.toDataURL() + '), auto';
}

这是我当前的代码:http://jsfiddle.net/Vw4yD/

function init(){
    var elem = document.getElementById('myCanvas'),
    elemLeft = elem.offsetLeft,
    elemTop = elem.offsetTop,
    context = elem.getContext('2d'),
    elements = [];

    //Spawn mouse on canvas enter.
    elem.addEventListener('mouseover', function() {
        makeCursor();
    });

    //Destroy mouse on canvas exit.
    elem.addEventListener('mouseout', function() {
        document.body.style.cursor = 'auto';
    });

    // Add event listener for `click` events.
    elem.addEventListener('click', function(event) {
        var x = event.pageX - elemLeft,
            y = event.pageY - elemTop;

        var brushHeight = document.getElementById('brushHeight').value;
        var brushWidth = document.getElementById('brushWidth').value;
        var brushColor = document.getElementById('brushColor').value;

        // Render elements.
        elements.forEach(function(element) {
            //Listen for controls.
            context.fillStyle = brushColor;
            context.fillRect(x, y, brushWidth, brushHeight);
        });
        // Add element.
        elements.push({
            colour: brushColor,
            width: brushWidth,
            height: brushHeight,
        });
    }, false);

    //Draw Mouse.
    function makeCursor() {
        var cursor = document.createElement('canvas'),
            cursorctx = cursor.getContext('2d');

        var x = event.pageX - elemLeft,
            y = event.pageY - elemTop;

        var cursorLeft = cursor.offsetLeft;
            cursorRight = cursor.offsetTop;

        var brushHeight = document.getElementById('brushHeight').value;
        var brushWidth = document.getElementById('brushWidth').value;
        var brushColor = document.getElementById('brushColor').value;

        cursor.width = brushWidth;
        cursor.height = brushHeight;

        cursorctx.fillStyle = brushColor;
        cursorctx.fillRect(x, y, brushWidth, brushHeight);
        cursorctx.fill();

        document.body.style.cursor = 'url(' + cursor.toDataURL() + '), auto';
    }
};

鼠标应该在进入画布时调整大小(它会)并改变颜色,你们知道如何让它工作吗?它让我难过,我已经研究了大约一整天,无论如何都找不到使用矩形来完成它,抚摸作品,但对于我需要完成的基本工作来说太复杂了。对不起,如果这篇文章写得不好,我过去一天睡得很少。

【问题讨论】:

    标签: javascript html canvas cursor html5-canvas


    【解决方案1】:

    给你http://jsfiddle.net/Vw4yD/1/

    我删除了光标的xy。它们应该等于 0,因为它相对于光标画布,而不是主画布。

    【讨论】:

    • TYVM!不知道为什么我首先添加了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多