JS + Canvas畫圖Demo

来源:https://www.cnblogs.com/qiantao/archive/2019/11/29/11958740.html
-Advertisement-
Play Games

直接上代碼,複製粘貼就能用: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF ...


直接上代碼,複製粘貼就能用:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="Public/jquery.min.js" type="text/javascript"></script>
<style type="text/css">
    canvas {
    background:#ccc;
    display:block;
    margin:0 auto;
}
#controls {
    width:200px;
    height:100%;
    position:absolute;
    left:0;
    top:0;
    background:linear-gradient(to bottom,#000000,#b8b8b8);
    user-select:none;
}
#controls section {
    margin-top:10px;
    height:20px;
}
#controls .label {
    width:50%;
    height:20px;
    line-height:20px;
    text-align:center;
    color:#FFF;
    display:block;
    float:left;
}
#xing {
    float:right;
    width:50%;
    height:20px;
}
/*#shape {
    */
        /*width:50%;
    height:20px;
    display:block;
    */
        /*
}
*/
        #controls .color {
    width:50%;
    height:auto;
    float:right;
}
#colors input {
    float:right;
    width:48%;
    height:20px;
    border:none;
}
#widths input {
    float:right;
    width:49%;
    height:20px;
    border:none;
}
#style {
    float:right;
    width:49%;
    height:20px;
    border:none;
}
input[type=button] {
    width:150px;
    height:30px;
    background:#C40000;
    color:#FFF;
    border-radius:5px;
    margin-top:10px;
    margin-left:10px;
    border:none;
    display:block;
}
</style>
</head>
<body>
    <canvas width="500" height="500"></canvas>
<div id="controls">
 
    <section id="shape">
        <label for="shape" class="label">選擇形狀 : </label>
        <select id="xing">
            <option value="rect">矩形</option>
            <option value="line">直線</option>
            <option value="circle">內切圓</option>
            <option value="circle1">外接圓</option>
            <option value="circle2">中心圓</option>
            <option value="poly">多邊形</option>
            <option value="pen">鉛筆</option>
            <option value="eraser">橡皮</option>
        </select>
    </section>
 
    <section id="colors">
        <label for="color" class="label">選擇顏色 : </label>
        <input type="color" id="color">
    </section>
    <section id="widths">
        <label for="color" class="label">選擇線寬 : </label>
        <input type="number" id="width" value="2" step="2" min="2" max="20">
    </section>
    <section id="styles">
        <label for="shape" class="label">選擇方式 : </label>
        <select id="style">
            <option value="stroke">描邊</option>
            <option value="fill">填充</option>
        </select>
    </section>
    <section id="sides">
        <label for="side" class="label">選擇邊數 : </label>
        <input type="number" id="side" value="3" min="3" max="20">
    </section>
    <input type="button" value="撤銷" id="redo">
    <input type="button" value="保存" id="save">
    <input type="button" value="清空" id="qingkong">
</div>
</body>
<script type="text/javascript">
var canvas = document.querySelector("canvas");
var cobj = canvas.getContext("2d");
var shape = document.querySelector("#xing");
var color = document.querySelector("#color");
var width = document.querySelector("#width");
var style = document.querySelector("#style");
var side = document.querySelector("#side");
var redo = document.querySelector("#redo");
var save = document.querySelector("#save");
var qingkong = document.querySelector("#qingkong");
console.log(side);
var data = [];
var s = "rect";
shape.onchange = function() {
    s = this.value;
};
var c = "#000";
color.onchange = function() {
    c = this.value;
};
var w = "2";
width.onchange = function() {
    w = this.value;
};
var st = "stroke";
style.onchange = function() {
    st = this.value;
};
var sd = "3";
side.onchange = function() {
    sd = this.value;
};

canvas.onmousedown = function(e) {
    var ox = e.offsetX;
    var oy = e.offsetY;
    var draw = new Draw(cobj, {
        color: c,
        width: w,
        style: st,
        side: sd
    });
    if (s == "pen") {
        cobj.beginPath();
        cobj.moveTo(ox, oy);
    }
    canvas.onmousemove = function(e) {
        var mx = e.offsetX;
        var my = e.offsetY;
        if (s != "eraser") {
            cobj.clearRect(0, 0, 500, 500);
            if (data.length != 0) {
                cobj.putImageData(data[data.length - 1], 0, 0, 0, 0, 500, 500); //將某個圖像數據放置到畫布指定的位置上  後面四個參數可省略
            }
        }
        //            cobj.strokeRect(ox,oy,mx-ox,my-oy);
        // cobj.beginPath()
        draw[s](ox, oy, mx, my, sd);
    };
    document.onmouseup = function() {
        data.push(cobj.getImageData(0, 0, 500, 500)); //獲取畫布當中指定區域當中所有的圖形數據
        canvas.onmousemove = null;
        document.onmouseup = null;
    }
};
redo.onclick = function() {
    if (data.length == 0) {
        alert("無效操作");
        return;
    }
    cobj.clearRect(0, 0, 500, 500);
    data.pop();
    if (data.length == 0) {
        return;
    }
    cobj.putImageData(data[data.length - 1], 0, 0, 0, 0, 500, 500);
};
save.onclick = function() {
    var r = canvas.toDataURL();
    location.assign(r)
};
qingkong.onclick = function() {
    cobj.clearRect(0, 0, 500, 500);
    data = [];
}
class Draw {
    constructor(cobj, option) {
        this.cobj = cobj;
        this.color = option.color;
        this.width = option.width;
        this.style = option.style;
    }
    init() { //初始化
        this.cobj.strokeStyle = this.color;
        this.cobj.fillStyle = this.color;
        this.cobj.lineWidth = this.width;
    }
    rect(ox, oy, mx, my) {
        this.init();
        this.cobj.beginPath();
        this.cobj.rect(ox, oy, mx - ox, my - oy);
        this.cobj[this.style]();

    }
    line(ox, oy, mx, my) {
        this.init();
        this.cobj.beginPath();
        this.cobj.moveTo(ox, oy);
        this.cobj.lineTo(mx, my);
        this.cobj.stroke();
    }
    circle(ox, oy, mx, my) { //內切圓
        this.init();
        this.cobj.beginPath();
        /*        var r=Math.sqrt(Math.pow(mx-ox,2)+Math.pow(my-oy,2))/2;
                this.cobj.arc(ox+(mx-ox)/2,oy+(my-oy)/2,r,0,2*Math.PI);*/
        var r = Math.abs(mx - ox) > Math.abs(my - oy) ? Math.abs(my - oy) / 2 : Math.abs(mx - ox) / 2;
        this.cobj.arc(ox + (ox < mx ? r : -r), oy + (oy < my ? r : -r), r, 0, 2 * Math.PI);
        this.cobj[this.style]();
    }
    circle1(ox, oy, mx, my) { //外接圓
        this.init();
        this.cobj.beginPath();
        var r = Math.sqrt(Math.pow(mx - ox, 2) + Math.pow(my - oy, 2)) / 2;
        this.cobj.arc(ox + (mx - ox) / 2, oy + (my - oy) / 2, r, 0, 2 * Math.PI);
        this.cobj[this.style]();
    }
    circle2(ox, oy, mx, my) { //中心圓
        this.init();
        this.cobj.beginPath();
        var r = Math.sqrt(Math.pow(mx - ox, 
              
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 重建全庫索引: exec sp_msforeachtable 'DBCC DBREINDEX(''?'')' 更新全庫統計信息: --更新全部統計信息 exec sp_updatestats 實例反饋: 在實際項目中,出現過導入數據的存儲過程執行需要大概46分鐘,而通過更新全庫統計信息之後,只需要6 ...
  • 德 App 進行 Bundle 化後,由於業務的複雜性,Bundle 的數量非常多。而這帶來了一個新的問題——Bundle 之間的依賴關係錯綜複雜,需要進行管控,使 Bundle 之間的依賴保持在架構設計之下。 ...
  • Node的全局對象和全局變數、非同步、無阻塞IO?Node的技術架構和函數調用原理是什麼? ...
  • 什麼是CommonJS規範?它與NodeJS是什麼關係呢?CommonJS 的底層實現原理是什麼? ...
  • 1、清空所有行和所有列 $('#dgJGQuery').datagrid({ columns: [], url: '', data: [] }); 2、採用 datagrid-groupview 分組展示後,取消分組展示。 datagrid使用分組展示 是基於view屬性,把view屬性設置為gro ...
  • 響應式開發 1. 響應式開發原理 使用媒體查詢針對不同寬度的設備進行佈局和樣式的設置,從而適配不同設備的目的 | 設備劃分 | 尺寸區間 | | | | | 超小屏幕(手機) | =768px~=992px~=1200px | 2. 響應式佈局容器 響應式需要一個父級作為佈局容器,來配合子級元素來實 ...
  • Accept Encoding和Content Encoding Accept Encoding和Content Encoding是HTTP中用來對採用何種壓縮格式傳輸正文進行協定的一對header。工作原理如下: 瀏覽器發送請求,通過Accept Encoding帶上自己支持的內容編碼格式列表 服 ...
  • 作者:水濤 座右銘:天行健,君子以自強不息 自白:我寫博文上來蹭蹭就是乾,我突然覺得我需要幽默一點了,好了,下麵我們說正經的 一、官方定義: DefinePlugin 允許創建一個在 編譯 時可以配置的全局常量。這可能會對開發模式和生產模式的構建允許不同的行為非常有用。如果在開發構建中,而不在發佈構 ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...