威尼斯wns.9778官网 > 计算机教程 > js HTML5 Canvas绘制转盘抽奖

原标题:js HTML5 Canvas绘制转盘抽奖

浏览次数:109 时间:2019-08-30

本文实例为大家分享了js转盘抽奖的具体代码,供大家参考,具体内容如下

本文实例为大家分享了HTML5 Canvas实现的一个画板代码,供大家参考,具体内容如下

1.实现的基本效果

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>DEMO6:自定义画板</title>
</head>
<body>
<canvas id="canvas" width="600" height="300">
  浏览器不支持canvas  <!-- 如果不支持会显示这段文字 -->
</canvas>
<br/>
<button style="width:80px;background-color:yellow;" onclick='linecolor="yellow";'>YELLOW</button>
<button style="width:80px ;background-color:red;" onclick='linecolor="red";'>RED</button>
<button style="width:80px ;background-color:blue;" onclick='linecolor="blue";'>BLUE</button>
<button style="width:80px ;background-color:green;" onclick='linecolor="green";'>GREEN</button>
<button style="width:80px ;background-color:white;" onclick='linecolor="white";'>WHITE</button>
<button style="width:80px ;background-color:black;" onclick='linecolor="black";'>BLACK</button>
<br/>

<button style="width: 80px;background-color: white;" onclick="linw=4;">4PX</button>
<button style="width: 80px;background-color: white;" onclick="linw=8;">8PX</button>
<button style="width: 80px;background-color: white;" onclick="linw=16;">16PX</button>
<br/>

<button style="width: 80px;background-color: white;" onclick="copyimage();">EXPORT</button>

<br/>
<img src="" id="image_png" width="600px" height="300px">
<br/>

<script type="text/javascript">
  var canvas = document.getElementById('canvas'); //获取标签
  var ctx = canvas.getContext("2d"); 

  var fillStyle = "black";
  ctx.fillRect(0,0,600,300);
  var onoff = false; //按下标记
  var oldx = -10;
  var oldy = -10;
  //设置颜色
  var linecolor = "white";
  var linw = 4;
  canvas.addEventListener("mousemove",draw,true); //鼠标移动事件
  canvas.addEventListener("mousedown",down,false); //鼠标按下事件
  canvas.addEventListener("mouseup",up,false); //鼠标弹起事件
  function down(event){
    onoff = true;
    oldx = event.pageX - 10;
    oldy = event.pageY - 10;
  }
  function up(){
    onoff = false;
  }
  function draw(event){
    if (onoff==true) {
      var newx = event.pageX - 10;
      var newy = event.pageY - 10
      ctx.beginPath();
      ctx.moveTo(oldx,oldy);
      ctx.lineTo(newx,newy);
      ctx.strokeStyle = linecolor;
      ctx.lineWidth = linw;
      ctx.lineCap = "round";
      ctx.stroke();

      oldx = newx;
      oldy = newy;
    }
  }
  function copyimage(event)
  {
    var img_png_src = canvas.toDataURL("image/png"); //将画板保存为图片格式的函数
    document.getElementById("image_png").src = img_png_src;
  }

  </script> 
</body>
</html>

 图片 1

以上就是本文的全部内容,希望对大家的学习有所帮助。

2.主要的内容

您可能感兴趣的文章:

 •html5中canvas标签的使用
 •jQueryRotate.js旋转插件

3.主要html代码

 <body>
  <div class="content">
    <div class="wheel">
      <canvas class="item" id="wheelCanvas" width="422px" height="422px"></canvas>
      <img class="pointer" src="images/wheel-pointer.png"/>
    </div>
  </div>
  <div class="tips" >
    jason
  </div>
</body>
</html>

本文由威尼斯wns.9778官网发布于计算机教程,转载请注明出处:js HTML5 Canvas绘制转盘抽奖

关键词:

上一篇:浅谈toLowerCase和toLocaleLowerCase的区别

下一篇:没有了