熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

JS實現簡單的Canvas畫圖實例

2022-06-13   來源: JSP教程 
這篇文章介紹了JS實現簡單的Canvas畫圖實例代碼有需要的朋友可以參考一下  

  定義變量:
[javascript]

復制代碼 代碼如下:
var startX;
var startY;
var endX;
var endY;
var radius;
var canvas = documentgetElementById("myCanvas");
var context = canvasgetContext("d");
var select = documentgetElementsByTagName("select");
var startX;
var startY;
var endX;
var endY;
var radius;
var canvas = documentgetElementById("myCanvas");
var context = canvasgetContext("d");
var select = documentgetElementsByTagName("select");

  
函數部分:
[javascript]

復制代碼 代碼如下:

  
windowonload=function() {
    canvasonmousedown = function(e) {
    e = e || event;
    startX = eclientX;
    startY = eclientY;
    if(select[]value == "arc") {
        canvasonmousemove = moveShowArc;
    } else {
        canvasonmousemove = moveShowRect;
    }
    }
    canvasonmouseup = function() {
    canvasonmousemove = "";
    }
}
function moveShowRect(e) {
    contextclearRect( );
    endX = eclientX startX;
    endY = eclientY startY;
    contextbeginPath();
    contextrect(startX startY endX endY);
    contextfillStyle = "#EDFF";
    contextfill();
    contextlineWidth = ;
    contextstrokeStyle = "black";
    contextstroke();
}
function moveShowArc(e) {
    contextclearRect( );
    endX = eclientX startX;
    endY = eclientY startY;
    radius = Mathsqrt(Mathpow(endX)+Mathpow(endY));
    contextbeginPath();
    contextarc(startX startYradius * MathPIfalse);
    contextfillStyle = "#EDFF";
    contextfill();
    contextlineWidth = ;
    contextstrokeStyle = "black";
    contextstroke();
}
windowonload=function() {
    canvasonmousedown = function(e) {
 e = e || event;
 startX = eclientX;
 startY = eclientY;
 if(select[]value == "arc") {
     canvasonmousemove = moveShowArc;
 } else {
     canvasonmousemove = moveShowRect;
 }
    }
    canvasonmouseup = function() {
 canvasonmousemove = "";
    }
}
function moveShowRect(e) {
    contextclearRect( );
    endX = eclientX startX;
    endY = eclientY startY;
    contextbeginPath();
    contextrect(startX startY endX endY);
    contextfillStyle = "#EDFF";
    contextfill();
    contextlineWidth = ;
    contextstrokeStyle = "black";
    contextstroke();
}
function moveShowArc(e) {
    contextclearRect( );
    endX = eclientX startX;
    endY = eclientY startY;
    radius = Mathsqrt(Mathpow(endX)+Mathpow(endY));
    contextbeginPath();
    contextarc(startX startYradius * MathPIfalse);
    contextfillStyle = "#EDFF";
    contextfill();
    contextlineWidth = ;
    contextstrokeStyle = "black";
    contextstroke();
}


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19820.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.