アプリ版:「スタンプのみでお礼する」機能のリリースについて

こんにちは。
勉強の為、プログラムの作成中に気になる動作がありました。
style.backgroundでrgbで赤を設定したはずなのに深い緑色が表示されてしまいますが、何が原因なのでしょうか?
以下は学習で作っていたプログラムです。


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<meta http-equiv="content-script-type" content="text/javascript">
<title>test</title>
<script type="text/javascript">
<!--

function Graphics(obj){
varobj=obj ? obj : document.createElement('div');
var canvas=(obj.ownerDocument) ?
obj : (
document.getElementById(obj) ? document.getElementById(obj) : document.createElement('div')
);

canvas.style.position="absolute";
canvas.style.top="0px";
canvas.style.left="0px";
canvas.style.width="10px";
canvas.style.height="10px";
}
Graphics.prototype={
setColor:function(r,g,b){// 色設定用(1)
canvas.style.background=rgb;
},setColor:function(color){// 色設定用(2)
canvas.style.background=color;
},setSize:function(sx ,sy ,ex ,ey){// 線画領域設定用
canvas.style.top=sx + "px";
canvas.style.left=sy + "px";
canvas.style.width=ex + "px";
canvas.style.height=ey + "px";
}
}

window.onload = function(){
var g=new Graphics("canvas");// 存在するIDを入れた時
//var g=new Graphics("canvas2");// 存在しないIDを入れた時
//var g=new Graphics();// 引数が空の時
g.setColor(255,0,0);
g.setSize(100,100,100,100);

}
// -->
</script>
</head>
<body>
<div id="canvas"></div>
てst
</body>
</html>

問題が発生したブラウザはIE6.0です。
以上、よろしくお願いいたします。

A 回答 (1件)

定義がダブりと


r,g,bがrgbに!これはまずいべ。
Graphics.prototype = {
setColor : function(){ // 色設定用(1)
if(arguments.length==3)
canvas.style.backgroundColor = (arguments[0]<<16)+(arguments[1]<<8)+arguments[2];
else if(arguments.length==1)
canvas.style.backgroundColor = arguments[0];
},
    • good
    • 0
この回答へのお礼

スイマセン…。一行書き忘れました…。
canvas.style.background = rgb;
の上の行に
rgb = "rgb("+ r +"," + g+ "," + b+ ")";

という式を書いていたのです。。。T-T

rgbを使わないで記述出来るんですね。勉強になりました!!
ありがとうございます!>w<

お礼日時:2008/10/07 16:42

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!