• 绘图


    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Canvas</title>
        <style>
          body {
            margin: 0;
            overflow: hidden;
            background: #ccc;
          }
          .toolbar {
            width: 150px;
            height: 75px;
            background: #ccc;
            padding: 5px;
          }
          input[type="color"], button {
            width: 90%;
            margin: 0 auto;
            display: block;
          }
          input[type="range"] {
            width: 70%;
          }
           span {
             position: relative;
             bottom: 5px;
           }
        </style>
      </head>
      <body>
        <div class="toolbar">
          <input type="color" aria-label="select pen color">
          <input type="range" min="2" max="50" value="30" aria-label="select pen size"><span class="output">30</span>
          <button>Clear canvas</button>
        </div>
    
        <canvas class="myCanvas">
          <p>Add suitable fallback here.</p>
        </canvas>
    
        <script>
          var canvas = document.querySelector('.myCanvas');
          var width = canvas.width = window.innerWidth;
          var height = canvas.height = window.innerHeight-85;
          var ctx = canvas.getContext('2d');
          ctx.fillStyle = 'rgb(0,0,0)';
          ctx.fillRect(0,0,width,height);
          var colorPicker = document.querySelector('input[type="color"]');
          var sizePicker = document.querySelector('input[type="range"]');
          var output = document.querySelector('.output');
          var clearBtn = document.querySelector('button');
          // covert degrees to radians
          function degToRad(degrees) {
            return degrees * Math.PI / 180;
          };
          // update sizepicker output value
          sizePicker.oninput = function() {
            output.textContent = sizePicker.value;
          }
          // store mouse pointer coordinates, and whether the button is pressed
          var curX;
          var curY;
          var pressed = false;
          // update mouse pointer coordinates
          document.onmousemove = function(e) {
            curX = (window.Event) ? e.pageX : e.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
            curY = (window.Event) ? e.pageY : e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
          }
          canvas.onmousedown = function() {
            pressed = true;
          };
          canvas.onmouseup = function() {
            pressed = false;
          }
          clearBtn.onclick = function() {
            ctx.fillStyle = 'rgb(0,0,0)';
            ctx.fillRect(0,0,width,height);
          }
          function draw() {
            if(pressed) {
              ctx.fillStyle = colorPicker.value;
              ctx.beginPath();
              ctx.arc(curX, curY-85, sizePicker.value, degToRad(0), degToRad(360), false);
              ctx.fill();
            }
            requestAnimationFrame(draw);
          }
          draw();
        </script>
      </body>
    </html>
  • 相关阅读:
    Web Storage中的sessionStorage和localStorage
    Javascrip获取页面URL信息
    SqlServer 查看备份文件中逻辑文件信息的Sql语句
    实用ExtJS教程100例-011:ExtJS Form 使用JSON数据赋值和取值
    实用ExtJS教程100例-010:ExtJS Form异步加载和提交数据
    实用ExtJS教程100例-009:ExtJS Form无刷新文件上传
    实用ExtJS教程100例-008:使用iframe填充ExtJS Window组件
    实用ExtJS教程100例-007:ExtJS中Window组件最小化
    实用ExtJS教程100例-006:ExtJS中Window的用法示例
    实用ExtJS教程100例-005:自定义对话框Ext.MessageBox.show
  • 原文地址:https://www.cnblogs.com/xiaobaizitaibai/p/10868043.html
Copyright © 2020-2023  润新知