W3C home > Mailing lists > Public > whatwg@whatwg.org > April 2008

[whatwg] Request: Canvas Tag CSS

From: Thomas Broyer <t.broyer@gmail.com>
Date: Mon, 7 Apr 2008 22:35:45 +0200
Message-ID: <a9699fd20804071335p6fff0d12k54e3385311b10c9@mail.gmail.com>
On Mon, Apr 7, 2008 at 9:08 PM, Greg Houston wrote:
> >  What if some script changes the rules?
> >  Do you want the UA to infer the dependencies
> >  and redraw the whole picture?
> >  Canvas is an imperative element, and that is on purpose.
> >  You want to mix a declarative mechanism in.  That is bad style.
> >
> >  Chris
>
> No. The UA would not redraw the canvas.
>
>
> We have a CSS rule:
> .myBox {
>      fill-style: rgba(255,0,0,1);
> }
>
> We have a function:
> function drawBox() {
>       var ctx = canvas.getContext('2d');
>       ctx.clearRect(0, 0, 0, 0);
>       ctx.fillStyle = css(myBox);
>       ctx.fillRect(0,0,25,25);
> }
>
> We run the function:
> drawBox(); // Draws a red square.
>
> ...
>
> Later the CSS rule is changed, either via the DOM or through the
> addition of a new style sheet that trumps the first rule.
>
> .myBox {
>      fill-style: rgba(0,255,0,1);
> }
>
> The next time the application runs drawBox(), this value is used and
> the box is green.

Your css() function could be written using the DOM-Level-2-Style, but
the easiest is to have elements with the styles you want and then use
getComputedStyle (as suggested by Anne)

<script>
var css = null;
if (getComputedStyle in window) {
    css = function (id, prop) {
        return window.getComputedStyle(document.getElementById(id))[prop];
    }
} else {
    // assume IE, with runtimeStyle
    css = function(id, prop) {
        return document.getElementById(id).runtimeStyle[prop];
    }
}
</script>

<span id=myBoxStyleHolder class=myBox style=display:none></span>

<script>
function drawBox() {
    var ctx = canvas.getContext('2d');
    ctx.clearRect(0, 0, 0, 0);
    ctx.fillStyle = css("myBoxStyleHolder", "backgroundColor");
    ctx.fillRect(0,0,25,25);
}
</script>


Your problem here is that fill-style is not a CSS property...


-- 
Thomas Broyer
Received on Monday, 7 April 2008 13:35:45 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 16:59:01 UTC