JS基于ocanvas插件實(shí)現(xiàn)的簡(jiǎn)單畫板效果代碼(附demo源碼下載)
來(lái)源:易賢網(wǎng) 閱讀:1834 次 日期:2016-07-08 15:22:35
溫馨提示:易賢網(wǎng)小編為您整理了“JS基于ocanvas插件實(shí)現(xiàn)的簡(jiǎn)單畫板效果代碼(附demo源碼下載)”,方便廣大網(wǎng)友查閱!

本文實(shí)例講述了JS基于ocanvas插件實(shí)現(xiàn)的簡(jiǎn)單畫板效果。分享給大家供大家參考,具體如下:

使用ocanvas做了個(gè)簡(jiǎn)單的在線畫板。

效果如下:

名單

<p align=center><img src="圖片網(wǎng)址" alt="名單"></p>

主要代碼如下:

<!DOCTYPE html>

<html>

<head>

  <meta charset="UTF-8" />

  <title>oCanvas Example</title>

  <meta name="robots" content="noindex, nofollow">

  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

  <meta name="apple-mobile-web-app-capable" content="yes" />

  <meta name="apple-mobile-web-app-status-bar-style" content="black" />

  <script src="http://libs.useso.com/js/zepto/1.1.3/zepto.min.js"></script>

  <script>

  var line_color = '#000';

  var line_size = 3;

  $(function(){

    $('.tool .color div').click(function(){

      $('.tool .color div').removeClass('active');

      $(this).addClass('active');

      line_color = $(this).data('color');

      mouseDot.fill = line_color;

    });

    $('.tool .size div').click(function(){

      $('.tool .size div').removeClass('active');

      $(this).addClass('active');

      line_size = $(this).data('size');

      mouseDot.radius = Math.max(line_size / 2, 3);

    });

  });

  </script>

  <style>

  body, html {padding:0; margin:0; overflow:hidden;}

  .tool{position:absolute; top:10px; left:10px; border:solid 1px #aaa; background-color:#eee; border-radius:5px; padding-right:5px;}

  .tool .color {float:left; margin:5px; width:30px;}

  .tool .color div{width:24px; height:24px; border:solid 2px #aaa; margin-bottom:5px; opacity:0.5;}

  .tool .color div:hover{opacity:1; cursor:pointer;}

  .tool .color .active{opacity:1; border:solid 2px #000;}

  .tool .size {float:left; margin:5px; width:30px; margin-left:0;}

  .tool .size div{width:30px; height:30px; border:solid 2px #aaa; margin-bottom:5px; opacity:0.5;}

  .tool .size div:hover{opacity:1; cursor:pointer;}

  .tool .size .active{opacity:1; border:solid 2px #000;}

  .tool .size span{display:block; margin:3px auto; height:24px; background-color:black;}

  .btn {clear:both; margin-bottom:5px; text-align:center;}

  .btn input {padding:3px 15px;}

  </style>

</head>

<body>

  <canvas id="canvas" width="200" height="100"></canvas>

  <div class="tool">

    <div class="color">

      <div style="background:#000;" data-color="#000" class="active"></div>

      <div style="background:#f00;" data-color="#f00"></div>

      <div style="background:#0f0;" data-color="#0f0"></div>

      <div style="background:#00f;" data-color="#00f"></div>

      <div style="background:#ff0;" data-color="#ff0"></div>

      <div style="background:#0ff;" data-color="#0ff"></div>

      <div style="background:#f0f;" data-color="#f0f"></div>

      <div style="background:#fff;" data-color="#fff"></div>

    </div>

    <div class="size">

      <div class="active" data-size="3" ><span style="width:3px;" ></span></div>

      <div data-size="6" ><span style="width:6px;" ></span></div>

      <div data-size="9" ><span style="width:9px;" ></span></div>

      <div data-size="12"><span style="width:12px;"></span></div>

      <div data-size="15"><span style="width:15px;"></span></div>

      <div data-size="20"><span style="width:20px;"></span></div>

      <div data-size="25"><span style="width:25px;"></span></div>

    </div>

    <div class="btn">

      <input type="button" value="清 空" onclick="clearAll();" />

    </div>

  </div>

  <script src="js/ocanvas-2.7.3.min.js"></script>

  <script>

  var c = document.querySelector("#canvas"),

  ctx = c.getContext("2d");

  c.width = window.innerWidth;

  c.height = window.innerHeight;

  c.addEventListener("touchmove", function (e) { e.preventDefault(); }, false);

  var cs = oCanvas.create({

    canvas: "#canvas",

    background: "#fff",

    fps: 30,

    disableScrolling: true

  });

  var isDraw = false;

  var xx = 0;

  var yy = 0;

  var mouseDot;

  clearAll();

  cs.bind('mousedown', function(){

    drawBegin(cs.mouse.x, cs.mouse.y);

  }).bind('touchstart tap', function(){

    drawBegin(cs.touch.x, cs.touch.y);

  }).bind('mouseup touchend', function(){

    isDraw = false;

  }).bind('mousemove', function(){

    drawMove(cs.mouse.x, cs.mouse.y);

  }).bind('touchmove', function(){

    drawMove(cs.touch.x, cs.touch.y);

  });

  /*

  cs.setLoop(function () {

    mouseDot.x = cs.mouse.x;

    mouseDot.y = cs.mouse.y;

  }).start();

  */

  function drawBegin(x, y)

  {

    isDraw = true;

    xx = x;

    yy = y;

    var dot = cs.display.arc({

      x: x,

      y: y,

      radius: line_size / 2,

      start: 0,

      end: 360,

      fill: line_color

    });

    cs.addChild(dot);

  }

  function drawMove(x, y)

  {

    if (isDraw)

    {

      var line = cs.display.line({

        start: { x: xx, y: yy },

        end: { x: x, y: y },

        stroke: '' + line_size + 'px ' + line_color,

        cap: "round"

      });

      cs.addChild(line);

      xx = x;

      yy = y;

    }

    else

    {

      mouseDot.moveTo(x, y);

      cs.addChild(mouseDot);

      cs.draw.redraw();

    }

  }

  function clearAll()

  {

    cs.reset();

    // 處理鼠標(biāo)

    cs.mouse.hide();

    mouseDot = cs.display.arc({

      x: -100,

      y: -100,

      radius: Math.max(line_size / 2, 3),

      start: 0,

      end: 360,

      fill: line_color,

      shadow: '0 0 5px #333'

    });

    cs.addChild(mouseDot);

  }

  </script>

</body>

</html>

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)