如何使用CSS设置canvas元素的样式

Mir*_*kov 6 html javascript css canvas

我在HTML画布中绘制了很多图形和元素.所有这些都有不同的颜色,笔画等.我真的不喜欢所有这些值都在我的JS代码中游荡,因为一些样式在CSS中,一些在代码中.

有人知道在CSS中定义样式并在实际渲染对象时读取样式的好方法吗?

以下是我需要做的一些示例:

context.beginPath();
context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
context.fillStyle = 'green'; // I'd like to set with CSS
context.fill();
context.lineWidth = 5; // I'd like to set with CSS
context.strokeStyle = '#003300'; // I'd like to set with CSS
context.stroke();
Run Code Online (Sandbox Code Playgroud)

http://jsfiddle.net/nedyalkov/ysgLqqh3/1/

Ask*_*ken 6

我参加派对有点晚了,但我只是有同样的情景,我解决了这个问题:

// "Cache"
var classColors = {};

function getColor(className) {
    // Check for the color
    if (!classColors[className]) {

        // Create an element with the class name and add it to the dom
        $c = $('<div class="' + className + '"></div>').css('display', 'none');
        $(document.body).append($c);

        // Get color from dom and put it in the color cache
        classColors[className] = $c.css('color');

        // Remove the element from the dom again
        $c.remove();
    }

    // Return color
    return classColors[className];
}
Run Code Online (Sandbox Code Playgroud)

我只需要颜色,但如果需要,可以扩展缓存对象以保留更多颜色.你将从函数中返回一个完整的对象.以下代码根本未经过测试:

var classProperties = {};

function getPropeties(className) {
    // Check for the properties object
    if (!classProperties[className]) {

        // Create an element with the class name and add it to the dom
        $c = $('<div class="' + className + '"></div>').css('display', 'none');
        $(document.body).append($c);

        // Get needed stuff from the dom and put it in the cache
        // P.S. Didn't test if canvas names are the same as css names.
        // If not you'll have to translate it
        classProperties[className] = {
            fillStyle: $c.css('color'),
            lineWidth: $c.css('border-width'),
            strokeStyle: $c.css('border-style')
        }

        // Remove the element from the dom again
        $c.remove();
    }

    // Return properties
    return classProperties[className];
}
Run Code Online (Sandbox Code Playgroud)

  • +1 我很喜欢,毕竟这些优秀的人都说这是不可能的,你想出了一个非常简单的方法来做到这一点。 (3认同)

Dom*_*ino 5

通常,在画布上绘制大量内容的人会制作自己的具有样式属性的形状对象。例如: http: //jsfiddle.net/b93cc3ww/

context = document.getElementById("myCanvas").getContext("2d");

function Rectangle(params) {
    this.x = params.x || 0;
    this.y = params.y || 0;
    this.width = params.width || 0;
    this.height = params.height || 0;
    this.fillStyle = params.fillStyle || "#FFFFFF";
    this.strokeStyle = params.strokeStyle || "#000000";
    this.lineWidth = params.lineWidth || 0;
}

Rectangle.prototype.draw = function () {
    if (this.fillStyle) {
        context.fillStyle = this.fillStyle;
        context.fillRect(this.x, this.y, this.width, this.height)
    }
    if (this.strokeStyle && this.lineWidth) {
        context.strokeStyle = this.strokeStyle;
        context.lineWidth = this.lineWidth;
        context.strokeRect(this.x, this.y, this.width, this.height);
    }
}

rectangles = [
    new Rectangle({
        x: 10,
        y: 10,
        width: 300,
        height: 150,
        fillStyle: "#FF0000"
    }),
    new Rectangle({
        x: 250,
        y: 10,
        width: 100,
        height: 80,
        fillStyle: "#00FF00",
        strokeStyle: "#00AA00",
        lineWidth: 5
    }),
    new Rectangle({
        x: 10,
        y: 200,
        width: 250,
        height: 80,
        strokeStyle: "#FF0000",
        lineWidth: 1
    })
]

for (var i = 0; i < rectangles.length; ++i) {
    rectangles[i].draw();
}
Run Code Online (Sandbox Code Playgroud)

如果您喜欢 CSS 的工作方式,您可以创建一个形状对象,它可以采用“类”参数,然后将“类”列表存储在代码顶部的数组中。

  • 这只是对OP说的一种迂回方式:“不,你不能这样做。” OP想要使用实际的CSS来渲染画布,这是根本不可能的。 (4认同)