我的demo需要在每一次點擊前刷新才能正常的顯示圖片,另外頂部的文字有的時候不能正確的顯示,不知道是什么原因,希望大家可以幫我看一下,十分感謝!
<!DOCTYPE html>
<html>
?? ?<head>
?? ??? ?<meta charset="UTF-8">
?? ??? ?<title></title>
?? ??? ?<style type="text/css">
?? ??? ??? ?#buttons{margin:50px 10%;}
?? ??? ??? ?#buttons a{margin-top:50px;margin-right:10px;text-decoration:none;font-size:20px;color:black;padding:5px;border:1px solid black;}
?? ??? ?</style>
?? ?</head>
?? ?<body>
?? ??? ?<canvas id="canvas" height="800" width="1200" style="margin: 0 20%;border:1px solid black;">此瀏覽器不支持canvas,請更換瀏覽器</canvas>
?? ??? ?<div id="buttons">
?? ??? ??? ?<a href="#">source-over</a>
?? ??? ??? ?<a href="#">source-atop</a>
?? ??? ??? ?<a href="#">source-in</a>
?? ??? ??? ?<a href="#">source-out</a>
?? ??? ??? ?<a href="#">destination-over</a>
?? ??? ??? ?<a href="#">destination-atop</a>
?? ??? ??? ?<a href="#">destination-in</a>
?? ??? ??? ?<a href="#">destination-out</a>
?? ??? ??? ?<a href="#">lighter</a>
?? ??? ??? ?<a href="#">xor</a>
?? ??? ??? ?<a href="#">copy</a>?? ??? ?
?? ??? ?</div>
?? ??? ?<script type="text/javascript">
?? ??? ??? ?window.onload=function(){
?? ??? ??? ??? ?draw("source-over");
?? ??? ??? ??? ?var buttons=document.getElementById("buttons").getElementsByTagName("a");
?? ??? ??? ??? ?for(var i=0;i<buttons.length;i++){
?? ??? ??? ??? ??? ?buttons[i].onclick=function(){
?? ??? ??? ??? ??? ??? ?draw(this.text);
?? ??? ??? ??? ??? ??? ?return false;
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}
?? ??? ??? ?}?? ?
?? ??? ??? ??? ?function draw(compositeStyle){
?? ??? ??? ??? ??? ?var canvas = document.getElementById("canvas");
?? ??? ??? ??? ??? ?var context = canvas.getContext("2d");
?? ??? ??? ??? ??? ?/**清空畫布**/
?? ??? ??? ??? ??? ?context.clearRect(0,0,canvas.width,canvas.height);
?? ??? ??? ??? ??? ?/**標題**/
?? ??? ??? ??? ??? ?context.font="bold 40px sans-serif";
?? ??? ??? ??? ??? ?context.textAlign="center";
?? ??? ??? ??? ??? ?context.textBaseline="middle";
?? ??? ??? ??? ??? ?context.fillText("globalCompsitionOperation的值為:"+compositeStyle,canvas.width/2,60);
?? ??? ??? ??? ??? ?/**方框**/
?? ??? ??? ??? ??? ?context.fillStyle="blue";
?? ??? ??? ??? ??? ?context.fillRect(300,150,500,500);
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?context.globalCompositeOperation =compositeStyle;
?? ??? ??? ??? ??? ?/**三角形**/
?? ??? ??? ??? ??? ?context.fillStyle="red";
?? ??? ??? ??? ??? ?context.beginPath();
?? ??? ??? ??? ??? ?context.moveTo(700,250);
?? ??? ??? ??? ??? ?context.lineTo(1000,750);
?? ??? ??? ??? ??? ?context.lineTo(400,750);
?? ??? ??? ??? ??? ?context.closePath();
?? ??? ??? ??? ??? ?context.fill();
?? ??? ??? ??? ?}
?? ??? ??? ?
?? ??? ?</script>
?? ?</body>
</html>
2016-07-13
洗了個澡?。。栴}解決!是畫字的問題,因為globalCompsitionOperation的屬性,如destination-out只顯示先畫的沒有重復部分,也就是說其他部分全部被清理!解決方案用離屏canvas技術?。?!
2017-08-27
都是人才,很簡單的問題。這需要用clip()嗎
只需要在
var context = canvas.getContext("2d");
這段代碼之后再加入寬度和高度設置即可
canvas.width = 1200;
canvas.height = 800;
保證可行。
2017-01-03
剛剛也提了這個問題,就看到了這個問題解決了。不用離屏,用clip也可以,globalCompositeOperation影響的是canvas上已有的所有圖形跟要畫的圖形的關系處理,即使已有的圖形跟要畫的沒有重疊也會被影響,所以用clip限制將要畫的圖形范圍,就不會影響所有已有的圖形。
2016-09-07
需要使用context.clip() 對繪制區域進行一個剪輯出來,在剪輯區域進行繪制矩形和三角形
2016-08-18
6687877
2016-07-13
這個問題困擾了我一天了!初步判定是在canvas中畫字的緣故,但是具體問題在哪里我還沒有找到,找到告訴你。
2016-07-13
程序我也跑了一下,有同樣的問題,但找不到!