1. 程式人生 > >2.使用canvas實現簡單的畫直線橡皮筋式畫框

2.使用canvas實現簡單的畫直線橡皮筋式畫框

css樣式基本和之前的一樣,這裡就不提了,

HTML程式碼:

 <div id='controls'>
        Stroke color:
        <select id="strokeStyleSelect">
            <option value="red">red</option>
            <option value="green">green</option>
            <option value="blue">blue</option>
            <option value="orange">orange</option>
            <option value="cornflowerblue" selected>cornflowerblue</option>
            <option value = 'navy'>navy</option>
            <option value = 'purple'>purple</option>
        </select>
        <input id = 'guidewireCheckbox' type = 'checkbox'/>
        <input id = 'eraseAllButton' type = 'button' value = 'Erase all'/>
    </div>
<canvas id="canvas" width="500" height="500">
    你的瀏覽器不支援canvas
</canvas>
JS程式碼:
  var canvas = document.getElementById('canvas'),
        context = canvas.getContext('2d'),
        eraseAllButton = document.getElementById('eraseAllButton'),
        strokeStyleSelect = document.getElementById('strokeStyleSelect'),
        guidewireCheckbox = document.getElementById('guidewireCheckbox'),
        drawingSurfaceImageData,
        mousedown = {},
        rubberbandRect = {},
        dragging = false,
        guidewires = guidewireCheckbox.checked;
    function drawGrid(color,stepx,stepy){//畫網格線,可要可不要
        context.strokeStyle = color;
        context.lineWidth = 0.5;
        for(var i = stepx + 0.5;i < canvas.width;i += stepx){
            context.beginPath();
            context.moveTo(i , 0);
            context.lineTo(i , canvas.height);
            context.stroke();
        }
        for(var i = stepy + 0.5;i < canvas.height;i += stepy){
            context.beginPath();
            context.moveTo(0 , i);
            context.lineTo(canvas.width , i);
            context.stroke();
        }
    }
    function windowToCanvas(x , y){
        var bbox = canvas.getBoundingClientRect();
        return {x : x - bbox.left * (canvas.width / bbox.width) ,
            y : y - bbox.top * (canvas.height / bbox.height)};
    }
    function saveDrawingSurface(){
        drawingSurfaceImageData = context.getImageData(0 , 0 ,
            canvas.width , canvas.height);
    }
    function restoreDrawingSurface(){
        context.putImageData(drawingSurfaceImageData , 0 , 0);
    }
    function updateRubberbandRectangle(loc){
        rubberbandRect.width = Math.abs(loc.x - mousedown.x);
        rubberbandRect.height = Math.abs(loc.y - mousedown.y);
        if(loc.x > mousedown.x) rubberbandRect.left = mousedown.x;
        else rubberbandRect.left = loc.x;
        if(loc.y > mousedown.y) rubberbandRect.top = mousedown.y;
        else rubberbandRect.top = loc.y;
    }
    function drawRubberbandShape(loc){
        context.beginPath();
        context.moveTo(mousedown.x , mousedown.y);
        context.lineTo(loc.x , loc.y);
        context.stroke();
    }
    function updateRubberband(loc){
        updateRubberbandRectangle(loc);
        drawRubberbandShape(loc);
    }
    function drawHorizontalLine(y){
        context.beginPath();
        context.moveTo(0 , y + 0.5 );
        context.lineTo(canvas.width , y + 0.5);
        context.stroke();
    }
    function drawVerticalLine(x){
        context.beginPath();
        context.moveTo(x + 0.5 , 0);
        context.lineTo(x + 0.5 , canvas.height);
        context.stroke();
    }
    function drawGuidewires(x , y){
        context.save();
        context.strokeStyle = 'rgba(0,0,230,0.4)';
        context.lineWidth = 0.5;
        drawVerticalLine(x);
        drawHorizontalLine(y);
        context.restore();
    }
    canvas.onmousedown = function (e){
        var loc = windowToCanvas(e.clientX , e.clientY);
        e.preventDefault();
        saveDrawingSurface();
        mousedown.x = loc.x;
        mousedown.y = loc.y;
        dragging = true;
    };

    canvas.onmousemove = function (e){
        var loc;
        if(dragging){
            e.preventDefault();
            loc = windowToCanvas(e.clientX , e.clientY);
            restoreDrawingSurface();
            updateRubberband(loc);
            if(guidewires){
                drawGuidewires(loc.x , loc.y);
            }
        }
    };
    canvas.onmouseup = function (e){
        loc = windowToCanvas(e.clientX , e.clientY);
        restoreDrawingSurface();
        updateRubberband(loc);
        dragging = false;
    }
    eraseAllButton.onclick = function (e){
        context.clearRect(0 , 0 , canvas.width , canvas.height);
       // drawGrid('lightgray' , 10 , 10);
        context.strokeStyle = strokeStyleSelect.value;
        saveDrawingSurface();
    };
    strokeStyleSelect.onchange = function (e){
        context.strokeStyle = strokeStyleSelect.value;
    }
    guidewireCheckbox.onchange = function (e){
        guidewires = guidewireCheckbox.checked;
    }
    context.strokeStyle = strokeStyleSelect.value;
之後還會上傳關於多邊形繪畫。


相關推薦

2.使用canvas實現簡單直線橡皮筋畫框

css樣式基本和之前的一樣,這裡就不提了, HTML程式碼: <div id='controls'> Stroke color: <select id="strokeStyleSelect">

消息隊列 (2) java實現簡單的RabbtMQ

java實現 java cal png bit close 項目 rri XML 假設有如下問題:   1.如果消費者連接中斷,這期間我們應該怎麽辦?   2.如何做到負載均衡?   3.如何有效的將數據發送到相關的接收者?就是怎麽樣過濾   4.如何保證消費者收到完整正確

canvas 實現滑鼠出矩形

    <!doctype html> <html> <head> <meta charset="UTF-8"> <meta name="Generator" content="EditPlus®"> <m

使用canvas實現簡單動畫

大多數 Canvas 繪圖 API 都沒有定義在 <canvas> 元素本身上,而是定義在通過畫布的 getContext()獲得的一個“繪圖環境”物件上。實現如下星星的運動<!DOCTYPE html> <html> <head&g

canvas實現簡單的畫圖功能

var bot;//畫布div var X,Y,X1,Y1;//座標 var flag=0; var time;//定時器ID var color=0;//記住所選顏色 var lineW=2;//畫筆粗細

canvas 實現簡單的影象擴散

canvas  實現簡單的影象擴散 <!DOCTYPE HTML> <html> <body> <canvas id="myCanvas" width="8

canvas實現簡單的畫筆功能

<!DOCTYPE> <html><head><meta charset="UTF-8"><title>模仿筆畫</title><style type="text/css">#_canvas{background-color:

html5 canvas 實現簡單繪製折線圖

<!DOCTYPE html> <html> <head> <meta charset='utf-8'> <title>畫圖</title&g

canvas線API實現簡單地圖

做一個青海專案時,涉及到地圖,簡單顯示青海省個地區的用了一個svg外掛,要求顯示熱點資料的就用了百度的地圖API。地圖的確高大上。 回顧一、後臺老大哥負責跟客戶溝通的,突然有一天客戶要求說登入頁顯示地區的地圖要有3D效果。心裡頓時有一萬匹草泥馬奔過~~           

4.canvas實現多邊形橡皮筋繪圖功能

首先是html程式碼:<div id="control"> Stroke color: <select id="strokeStyleSelect"> <option value="red">red<

java實現簡單二維迷宮(2

blog 成員 new 構建 push port ava amp use 這次是改良版本。 將地圖封裝,老鼠封裝。是對Java基礎的一個練習吧。 這次實現也遇到了一些問題。主要是棧。封裝的mouse類中有成員變量i,j代表了老鼠的坐標。將mouse類對象m入棧的時候,總是入

一個在h5的canvas元素中撲克牌jquery插件實現

code isp arguments close tca func spa font blog 1 //非架構 2 ; (function ($) { 3 var aspect = 5.7 / 8.8;//撲克寬和高比例 4 function Paint

使用python實現簡單多邊形。

oop 簡單的 python bob int range turtle 半徑 tle import turtleimport timeimport mathdef polygon(bob,n,r): jiaoDu = 360/n juLi=2*r*math.si

BootStrap實現簡單響應導航菜單

bar query alt str content doctype bootstra inf ini    用BootStrap實現響應式導航欄,我會對其中的一些樣式進行說明。    先上代碼,是一個很簡單的Demo。     <!doctype html>

類Shiro權限校驗框架的設計和實現(2)--對復雜權限表達的支持

treemap evel 註冊 st3 builder row blog array www. 前言:   我看了下shiro好像默認不支持復雜表達式的權限校驗, 它需要開發者自己去做些功能擴展的工作. 針對這個問題, 同時也會為了彌補上一篇文章提到的支持復雜表示需求,

Java 2種方法實現簡單的session超時登出

    1、使用攔截器           使用者每次和後臺互動,如果使用者長時間未操作,則需要檢測使用者的登入狀態,這樣的場景已經是再正常不過了。   傳統的做法可以在每個controller裡先判斷user的狀態,然後再執行業務操作,但這樣比較程式

一個很簡單的基於棧過程虛擬機器的實現,它執行目標平臺【x86】的原生程式碼。

    本文提供的 “棧式過程虛擬機器” 的實現,掛在本人的 github 上面,對想要深入瞭解 “棧式過程虛擬機器” 的人,它或許可以起到一個不錯的作用,但是本人建議一般性瞭解就可以了,另外順帶一提:如果你想要依靠它維持生活,在咋們國家是不可能不現實的,到時候你就只有 “多冷的

資料結構2--線性表(java程式碼實現線性表的鏈儲存)

1.鏈式儲存       2.分析       每個節點為一個物件,該物件包含資料域和指標域        整條單鏈表為一個物件,他和節點物件進行組合。  3.

rem實現簡單的響應佈局

rem是什麼? rem(font size of the root element)是指相對於根元素的字型大小的單位。簡單的說它就是一個相對單位。看到rem大家一定會想起em單位,em(font size of the 

express實現簡單的增刪改查學習筆記(2)

接上一個(1),將會用express實現簡單的增刪改查的功能 1.在專案資料夾下新建data資料夾,此資料夾下新建data.json,裡面寫一個空的陣列 2.在專案資料夾下新建db.js const json = require('./data'); //是新生成的da