最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript 動(dòng)態(tài)三角函數(shù)實(shí)例詳解

 更新時(shí)間:2017年01月08日 09:36:08   作者:司馬懿字仲達(dá)  
本文通過(guò)實(shí)例代碼給大家實(shí)例講解了javascript動(dòng)態(tài)三角函數(shù)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下

下面一段代碼給大家分享JavaScript 動(dòng)態(tài)三角函數(shù),具體代碼如下所述:

 <html>
 <head>
 <meta charset="utf8" />
 <title>三角函數(shù)圖形</title>
 <style type="text/css">
  body {
  background-color:black;
  }
  #canvas {
  position:absolute;
  top:50%;
  left:50%;
  margin:-151px 0 0 -401px;
  border:1px dashed #171717;
  }
 </style>
 </head>
 <body>
 <canvas id="canvas" width="800px" height="300px">您的瀏覽器不支持canvas</canvas>
 <script type="text/javascript">
  //判斷是否支持canvaas
  function isSupportCanvas(canvas) {
  return !!(canvas.getContext && canvas.getContext("2d"));
  }
  //requestAnimationFrame會(huì)自動(dòng)使用最優(yōu)的幀率進(jìn)行渲染
  function setupRAF() {
  var lastTime = 0;
  //兼容各個(gè)瀏覽器,Internet Explorer11、Google Chrome(Microsoft Edge)、Mozilla Firefox、Opera
  var vendors = ["ms", "moz", "webkit", "o"];
  for(var i=0; i<vendors.length; i++) {
   window.requestAnimationFrame = window[vendors[i] + "RequestAnimationFrame"];
   window.cancelAnimationFrame = window[vendors[i] + "CancelAnimationFrame"] || window[vendors[i] + "CancelRequestAnimationFrame"];
   //測(cè)試瀏覽器支持哪一張
   if(window.requestAnimationFrame) {
   console.log(vendors[i] + "requestAnimationFrame");
   }
   if(window[vendors[i] + "CancelAnimationFrame"]) {
   console.log(vendors[i] + "CancelAnimationFrame");
   }
   if(window[vendors[i] + "CancelRequestAnimationFrame"]) {
   console.log(vendors[i] + "CancelRequestAnimationFrame");
   }
  }
  //回退機(jī)制
  if(!window.requestAnimationFrame) {
   window.requestAnimationFrame = function(callback, element) {
   var currentTime = new Date().getTime();
   var timeToCall = Math.max(0, 16-(currentTime-lastTime));
   var callTime = currentTime + timeToCall;
   var id = window.setTimeout(function() {
    callback(callTime);
   }, timeToCall);
   lastTime = callTime;
   return id;
   };
  }
  //回退機(jī)制
  if(!window.cancelAnimationFrame) {
   window.cancelAnimationFrame = function(id) {
   clearTimeout(id);
   }
  }
  }
  var CanvasController = function(canvas) {
  var ctx = canvas.getContext("2d");
  ctx.lineWidth = 1;
  ctx.textAlign = "left";
  ctx.textBaseline = "middle";
  ctx.font = "bold 18pt Calibri";
  var i = 0;
  var step = 1;
  var unitX = 90
  var unitY = canvas.height * 0.3;
  function update() {
   i += step;
   i %= 360;
  }
  //渲染坐標(biāo)
  function rendeRcoordinate() {
   ctx.strokeStyle = "white";
   ctx.beginPath();
   var topUnit = 0.5 * canvas.height - unitY;
   var bottomUnit = 0.5 * canvas.height + unitY;
   ctx.moveTo(0, topUnit);
   ctx.lineTo(canvas.width, topUnit);
   ctx.moveTo(0, bottomUnit);
   ctx.lineTo(canvas.width, bottomUnit);
   ctx.stroke();
  }
  //渲染三角函數(shù)
  function render(trigonometricFunction, color) {
   ctx.strokeStyle = color;
   ctx.beginPath();
   var isInRange = false;
   for(var x=0; x < canvas.width; x++) {
   var a = (x + i) / 180 * Math.PI;
   var y = trigonometricFunction(a);
   //tan值有可能是無(wú)窮大或無(wú)窮小
   if(isFinite(y)) {
    y = y * unitY + 0.5 * canvas.height;
    if(isInRange) {
    if(y < 0 || y > canvas.height) {
     isInRange = false;
    } else {
     ctx.lineTo(x, y);
    }
    } else {
    isInRange = true;
    ctx.moveTo(x, y);
    }
    if(x == 0) {
    ctx.fillStyle = color;
    var funcName = trigonometricFunction.toString();
    var reg = /function\s*(\w*)/i;
    var matches = reg.exec(funcName);
    ctx.fillText(matches[1], 0, y);
    } 
   } else {
    isInRange = false;
   }
   }
   ctx.stroke();
  }
  this.init = function() {
   function loop() {
   requestAnimationFrame(loop, canvas);
   ctx.clearRect(0, 0, canvas.width, canvas.height);
   update();
   rendeRcoordinate();
   render(Math.sin, "red");
   render(Math.cos, "green");
   render(Math.tan, "blue");
   }
   loop();
  }
  }
  function init() {
  var canvas = document.getElementById("canvas");
  if(!isSupportCanvas(canvas)) {
   return;
  }
  setupRAF();
  var canvasController = new CanvasController(canvas);
  canvasController.init();
  }
  init();
 </script>
 </body>
</html>
</html>

相關(guān)文章

  • 詳解JavaScript基于面向?qū)ο笾畡?chuàng)建對(duì)象(1)

    詳解JavaScript基于面向?qū)ο笾畡?chuàng)建對(duì)象(1)

    這篇文章主要介紹了JavaScript基于面向?qū)ο笾畡?chuàng)建對(duì)象,對(duì)創(chuàng)建對(duì)象進(jìn)行了詳細(xì)描述,感興趣的小伙伴們可以參考一下
    2015-12-12
  • JavaScript中EventBus實(shí)現(xiàn)對(duì)象之間通信

    JavaScript中EventBus實(shí)現(xiàn)對(duì)象之間通信

    這篇文章主要介紹了JavaScript中EventBus實(shí)現(xiàn)對(duì)象之間通信,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • 常用的 JS 排序算法 整理版

    常用的 JS 排序算法 整理版

    關(guān)于排序算法的問(wèn)題可以在網(wǎng)上搜到一大堆,但是純 JS 版比較零散,之前面試的時(shí)候特意整理了一遍,附帶排序效率比較
    2018-04-04
  • 微信小程序自定義組件Component的代碼詳解

    微信小程序自定義組件Component的代碼詳解

    這篇文章主要給大家介紹了關(guān)于微信小程序自定義組件Component的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2023-03-03
  • js+css實(shí)現(xiàn)換膚效果

    js+css實(shí)現(xiàn)換膚效果

    這篇文章主要為大家詳細(xì)介紹了js+css實(shí)現(xiàn)換膚效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07
  • JS使用tween.js動(dòng)畫(huà)庫(kù)實(shí)現(xiàn)輪播圖并且有切換功能

    JS使用tween.js動(dòng)畫(huà)庫(kù)實(shí)現(xiàn)輪播圖并且有切換功能

    本文通過(guò)實(shí)例代碼給大家介紹了JS使用tween.js動(dòng)畫(huà)庫(kù)實(shí)現(xiàn)輪播圖并且有切換功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-07-07
  • JS 自執(zhí)行函數(shù)原理及用法

    JS 自執(zhí)行函數(shù)原理及用法

    這篇文章主要介紹了JS 自執(zhí)行函數(shù)原理及技巧,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • 全面解析Bootstrap表單使用方法(表單按鈕)

    全面解析Bootstrap表單使用方法(表單按鈕)

    這篇文章全面解析了Bootstrap表單的使用方法,本文重點(diǎn)介紹Bootstrap表單按鈕,感興趣的小伙伴們可以參考一下
    2015-11-11
  • 一個(gè)JS函數(shù)搞定網(wǎng)頁(yè)標(biāo)題(title)閃動(dòng)效果

    一個(gè)JS函數(shù)搞定網(wǎng)頁(yè)標(biāo)題(title)閃動(dòng)效果

    這篇文章主要介紹了使用JS函數(shù)實(shí)現(xiàn)網(wǎng)頁(yè)標(biāo)題(title)閃動(dòng)效果的代碼,需要的朋友可以參考下
    2014-05-05
  • JavaScript實(shí)現(xiàn)簡(jiǎn)單的輪播圖效果

    JavaScript實(shí)現(xiàn)簡(jiǎn)單的輪播圖效果

    這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)簡(jiǎn)單的輪播圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06

最新評(píng)論

嘉义市| 徐州市| 永新县| 广汉市| 丹巴县| 明星| 延津县| 福鼎市| 都安| 山东省| 麦盖提县| 西林县| 申扎县| 沧州市| 横峰县| 合山市| 和静县| 苍南县| 新安县| 宜昌市| 兰考县| 通许县| 分宜县| 桐庐县| 海兴县| 宁强县| 洪江市| 新乡县| 新绛县| 仁布县| 莆田市| 进贤县| 尼勒克县| 巴楚县| 洱源县| 错那县| 九江市| 武强县| 毕节市| 凤台县| 侯马市|