/* 「SpinAnime」 2005/09/21版 THU madpoet@thu.sakura.ne.jp http://thu.sakura.ne.jp/ */ // SpinAnimeクラス function SpinAnime(id, folder, fileName, fileTail, fileFigure, width, height, cgNum, cutNum, wait, waitCtrl, waitMax, waitMin, waitWidth, order) { var i, node; this.Id = new Array(); this.Id[0] = id; this.Id[1] = id + '_1', this.Id[2] = id + '_2'; this.Id[3] = id + '_waitNum'; this.Folder = folder; this.FileName = fileName; this.FileTail = fileTail; this.FileFigure = fileFigure; this.Width = width, this.Height = height; this.CutNum = cutNum; if (this.Width < this.CutNum) { this.CutNum = this.Width; } this.CutWidth = Math.floor(this.Width / this.CutNum); if (this.CutWidth < 0) { this.CutWidth = 1; } this.CutCount = 0; this.CgNum = cgNum; this.Wait = wait; this.WaitMax = waitMax; this.WaitMin = waitMin; this.WaitWidth = waitWidth; this.Order = order, this.WaitCtrl = waitCtrl; if(!document.getElementById(this.Id[0])) { return false; } // 表示用画面(テーブル等)の生成 tagRemove(this.Id[0]); tagAppend(this.Id[0], tagCreate('table', this.Id[0] + '_frame', 'border', 0, 'cellSpacing', 0, 'cellPadding', 0)); tagAppend(this.Id[0] + '_frame', tagCreate('tbody', this.Id[0] + '_frameTbody')); tagAppend(this.Id[0] + '_frameTbody', tagCreate('tr', this.Id[0] + '_frameTr')); tagAppend(this.Id[0] + '_frameTr', tagCreate('td', this.Id[0] + '_frameTd', 'width', this.Width)); tagAppend(this.Id[0] + '_frameTd', tagCreate('table', this.Id[0] + '_table', 'border', 0, 'cellSpacing', 0, 'cellPadding', 0, 'width', this.Width, 'align', 'left')); tagAppend(this.Id[0] + '_table', tagCreate('tbody', this.Id[0] + '_tableTbody')); tagAppend(this.Id[0] + '_tableTbody', tagCreate('tr', this.Id[0] + '_tableTr1')); tagAppend(this.Id[0] + '_tableTr1', tagCreate('td', this.Id[0] + '_tableTd')); tagAppend(this.Id[0] + '_tableTd', tagCreate('img', this.Id[1], 'src', 'none.gif', 'alt', 'left CG', 'width', this.Width, 'height', this.Height)); tagAppend(this.Id[0] + '_tableTd', tagCreate('img', this.Id[2], 'src', 'none.gif', 'alt', 'right CG', 'width', '0', 'height', this.Height)); if (this.WaitCtrl == 'on') { tagAppend(this.Id[0] + '_tableTbody', tagCreate('tr', this.Id[0] + '_tableTr2')); tagAppend(this.Id[0] + '_tableTr2', tagCreate('td', this.Id[0] + '_tableTdWait', 'colSpan', 2, 'width', this.Width)); node = document.getElementById(this.Id[0] + '_tableTdWait'); node.style.fontSize = "10pt"; node.style.lineHeight = "10pt"; tagAppend(this.Id[0] + '_tableTdWait', tagCreate('span', this.Id[0] + '_waitUp', 'title', 'wait up')); node = document.getElementById(this.Id[0] + '_waitUp'); node.style.cursor = 'pointer'; node.onclick = this.waitChange; tagAppendText(node, '▲'); tagAppend(this.Id[0] + '_tableTdWait', tagCreate('span', this.Id[0] + '_waitDown', 'title', 'wait down')); node = document.getElementById(this.Id[0] + '_waitDown'); node.style.cursor = 'pointer'; node.onclick = this.waitChange; tagAppendText(node, '▼'); tagAppendText(this.Id[0] + '_tableTdWait', ' wait:'); tagAppend(this.Id[0] + '_tableTdWait', tagCreate('span', this.Id[0] + '_waitNum')); tagAppendText(this.Id[0] + '_waitNum', 'none'); } for (i = 1; i < 3; i++) { node = document.getElementById(this.Id[i]); node.title = 'Start & Stop'; node.style.cursor = 'pointer'; node.onclick = this.startAndStop; } if (this.WaitCtrl == 'on') { node = document.getElementById(this.Id[3]); node.firstChild.nodeValue = this.Wait; } this.PastCgNum = 0; if (this.Order == 'inOrder') { this.PresentCgNum = this.CgNum; } else { this.PresentCgNum = 0; } this.Node = new Array(); this.Node[0] = document.getElementById(this.Id[1]); this.Node[1] = document.getElementById(this.Id[2]); this.Node[0].src = this.Folder + this.FileName + figureSet(this.FileFigure, this.randomSelect()) + '.' + this.FileTail; this.Roll = true; var func = "changeMove('" + this.Id[0] + "')"; this.TimeId = setInterval(func, this.Wait); } // 次に表示する画像の選択 SpinAnime.prototype.randomSelect = function() { if (this.Order == 'inOrder') { num = this.PresentCgNum + 1; if (num > this.CgNum) { num = 1; } this.PresentCgNum = num; return num; } var i, j, num, randList; if (this.CgNum == 1) { num = 1; } else if (this.CgNum == 2) { if (this.PresentCgNum == 1) { num = 2; } else if (this.PresentCgNum == 2) { num = 1; } else { num = Math.ceil(Math.random() * this.CgNum) + 1; } } else { randList = new Array(); j = 0; for (i = 1; i <= this.CgNum; i++) { if (this.PresentCgNum == i) { continue; } else if (this.PastCgNum == i) { continue; } randList[j] = i; j++; } num = randList[Math.floor(Math.random() * randList.length)]; } this.PastCgNum = this.PresentCgNum; this.PresentCgNum = num; return num; } // 回転の開始と停止 SpinAnime.prototype.startAndStop = function() { var tmp = this.id.split('_'); var fieldId = tmp[0]; var sp = spinAnime[fieldId]; if(!(document.getElementById(sp.Id[0]))) { return false; } if (sp.Roll) { sp.Roll = false; clearInterval(sp.TimeId); } else { sp.Roll = true; var func = "changeMove('" + fieldId + "')" sp.TimeId = setInterval(func, sp.Wait); } } // ウェイトの増減 SpinAnime.prototype.waitChange = function() { var tmp = this.id.split('_'); var fieldId = tmp[0]; var sp = spinAnime[fieldId]; var node; if(!(node = document.getElementById(sp.Id[3]))) { return false; } if (this.id == sp.Id[0] + '_waitUp') { sp.Wait += sp.WaitWidth; if (sp.Wait > sp.WaitMax) { sp.Wait = sp.WaitMax; } else if (sp.Wait == sp.WaitWidth + sp.WaitMin) { sp.Wait = sp.WaitWidth; } } else { sp.Wait -= sp.WaitWidth; if (sp.Wait < sp.WaitMin) { sp.Wait = sp.WaitMin; } } node.firstChild.nodeValue = sp.Wait; if (sp.Roll) { clearInterval(sp.TimeId); var func = "changeMove('" + fieldId + "')"; sp.TimeId = setInterval(func, sp.Wait); } } // 回転アニメの実行 function changeMove(fieldId) { var sp = spinAnime[fieldId]; if (sp.CutCount == 0) { sp.Node[0].src = sp.Folder + sp.FileName + figureSet(sp.FileFigure, sp.PresentCgNum) + '.' + sp.FileTail; sp.Node[0].width = sp.Width; sp.Node[1].src = sp.Folder + sp.FileName + figureSet(sp.FileFigure, sp.randomSelect()) + '.' + sp.FileTail; sp.Node[1].width = '0'; } else { sp.Node[0].width -= sp.CutWidth; if (sp.Node[0].width < 0) { sp.Node[0].width = '0'; } sp.Node[1].width += sp.CutWidth; if (sp.Node[1].width > sp.Width) { sp.Node[1].width = sp.Width; } } sp.CutCount++; if (sp.CutCount >= sp.CutNum) { sp.CutCount = 0; } } // 数字の桁合わせ function figureSet(figure, num) { var i; var zero = '', compare = 10; for (i = 0; i < figure-1; i++) { zero += '0'; } num -= 0; for (i = 0; i < figure-1; i++) { if (num < compare) { num = zero + num; return num; } else { compare *= 10; zero = zero.substring(1); } } return num; } // テキストの追加 //(追加する場所のid or node, 追加するテキスト) function tagAppendText() { if (!arguments[0]) { return false; } var str = document.createTextNode(arguments[1]); var node; if (typeof(arguments[0]) == 'string') { node = document.getElementById(arguments[0]); } else { node = arguments[0]; } node.appendChild(str); return node; } // タグの生成 //(タグ名, id, 属性1, 属性値1, 属性2, 属性値2, etc...) function tagCreate() { if (!arguments[0]) { return false; } var tag = document.createElement(arguments[0]); if (arguments[1]) { tag.id = arguments[1]; } var i = 2; while(arguments[i] && arguments[i+1]) { tag.setAttribute(arguments[i], arguments[i+1]); i+=2; } return tag; } // タグの追加 //(追加する場所のid or node, 追加するタグ) function tagAppend() { if (!arguments[0]) { return false; } if (typeof(arguments[0]) == 'string') { document.getElementById(arguments[0]).appendChild(arguments[1]); return true; } else { arguments[0].appendChild(arguments[1]); return arguments[0]; } } // タグの追加 //(追加する場所のid or node, 追加するタグ) function tagAppend() { if (!arguments[0]) { return false; } if (typeof(arguments[0]) == 'string') { document.getElementById(arguments[0]).appendChild(arguments[1]); return true; } else { arguments[0].appendChild(arguments[1]); return arguments[0]; } } // タグの削除 //(削除するタグの親タグのid or node, 削除位置(all, head, foot, Number)) function tagRemove() { if (!arguments[0]) { return false; } var node; if (typeof(arguments[0]) == 'string') { node = document.getElementById(arguments[0]); } else { node = arguments[0]; } var numMax = node.childNodes.length - 1; if (numMax < 0) { numMax = 0; } if (!isNaN(arguments[1])) { var num = arguments[1]; if (num < 0) { num = 0; } else if (num > numMax) { num = numMax; } node.removeChild(node.childNodes[num]); } else if (!arguments[1] || arguments[1] == 'all') { while(true) { if(node.childNodes[0] != null) { node.removeChild(node.childNodes[0]); } else { break; } } } else if (arguments[1] == 'foot') { node.removeChild(node.childNodes[numMax]); } else { node.removeChild(node.childNodes[0]); } }