想试着做一个composite的演示,但是无法将<a>的文本赋到变量中,这是为什么呢?
这是html:
<!doctype html> <html> <head> <meta charset="utf-8"></meta> <title>合成属性演示</title> <script type="text/javascript" src="合成.js"></script> <style type="text/css"> #buttons{ width:600px; font:14px 微软雅黑; color: #0277bf; } #buttons a{ text-decoration: none; } </style> </head> <body> <canvas id='canvas'>看到这段文字请更换浏览器</canvas> <div id='buttons'> <a href="#">source-over</a> <a href="#">source-in</a> <a href="#">source-out</a> <a href="#">source-atop</a> <a href="#">destination-over</a> <a href="#">destination-in</a> <a href="#">destination-out</a> <a href="#">destination-atop</a> <a href="#">lighter</a> <a href="#">copy</a> <a href="#">xor</a> </div> </body> </html>
这是js:
window.onload = function(){ var canvas = document.getElementById('canvas'); var context = canvas.getContext('2d'); var buttons = document.getElementById('buttons'); var aS = buttons.getElementsByTagName('a'); var compositeType = 'source-out'; for(var i = 0;i<aS.length;i++){ aS[i].onclick = function(){ compositeType = this.text; } } canvas.width = 600; canvas.height = 400; context.globalAlpha = 1; context.globalCompositeOperation = compositeType; context.beginPath(); context.rect(100,100,200,100); context.fillStyle = 'red'; context.fill(); context.closePath(); context.beginPath(); context.arc(300,200,75,0,2*Math.PI); context.fillStyle = 'blue'; context.fill(); context.closePath(); }