HTML5画布圈滞后显示

时间:2018-08-08 16:59:23

标签: javascript html html5 canvas owl-carousel

我正在尝试在基本的猫头鹰caroussel上设置一些画布圆百分比值,当滚动时我面临的问题是滞后,有些值直到它们到达第一个位置时才显示卡罗素,有谁知道如何解决这个问题?

下面有一个图片,为解释我的意思,我还添加了代码段代码,以便您可以查看。

谢谢大家。

enter image description here

/* OWL CAROUSSEL JS*/

$(document).ready(function() {
  $(".owl-carousel").owlCarousel({
    loop: true,
    margin: 20,
    nav: true,
    dots: true,
    responsive: {
      0: {
        items: 1
      },
      600: {
        items: 3
      },
      1000: {
        items: 5
      }
    }
  });
});

/* CANVAS JS */
function progressSim(id, percent) {
  var ctx = document.getElementById(id).getContext('2d'),
    cw = ctx.canvas.width,
    ch = ctx.canvas.height,
    al = 0,
    sim = setInterval(progress, 25);

  function progress() {
    var start = 4.72,
      diff = ((al / 100) * Math.PI * 2 * 10).toFixed(2);

    ctx.clearRect(0, 0, cw, ch);
    ctx.lineWidth = 7;
    ctx.fillStyle = '#000';
    ctx.strokeStyle = "#000";
    ctx.textAlign = 'center';
    ctx.font = "30px Arial";
    ctx.fillText(al + '%', cw * .5, ch * .5 + 2, cw);
    ctx.beginPath();
    ctx.arc(80, 80, 70, start, diff / 10 + start, false);
    ctx.stroke();
    if (al >= percent) {
      clearTimeout(sim);
      // Add scripting here that will run when progress completes
    }
    al++;
  };
}

progressSim('my_canvas1', 65);
progressSim('my_canvas2', 80);
progressSim('my_canvas3', 90);
progressSim('my_canvas4', 15);
progressSim('my_canvas5', 35);
progressSim('my_canvas6', 68);
progressSim('my_canvas7', 89);
progressSim('my_canvas8', 14);
.item {
  width: 200px;
  height: 230px;
  background-color: orange;
  padding: 10px 10px 10px 10px;
}

#container {
  width: 100%;
  height: 550px;
  background-color: #d82c2e;
  position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<head>
  <!--BS CSS-->
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

  <!--BS JS-->
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>


  <!--OWL CSS-->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.carousel.min.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/assets/owl.theme.default.min.css">

</head>

<body>
  <div id="=container">
    <div class="owl-carousel owl-theme">
      <div class="item">
        <h4>Value 1 </h4><canvas id="my_canvas1" width="170" height="170"></canvas></div>
      <div class="item">
        <h4>Value 2 </h4><canvas id="my_canvas2" width="170" height="170"></canvas></div>
      <div class="item">
        <h4>Value 3 </h4><canvas id="my_canvas3" width="170" height="170"></canvas></div>
      <div class="item">
        <h4>Value 4 </h4><canvas id="my_canvas4" width="170" height="170"></canvas></div>
      <div class="item">
        <h4>Value 5 </h4><canvas id="my_canvas5" width="170" height="170"></canvas></div>
      <div class="item">
        <h4>Value 6 </h4><canvas id="my_canvas6" width="170" height="170"></canvas></div>
      <div class="item">
        <h4>Value 7 </h4><canvas id="my_canvas7" width="170" height="170"></canvas></div>
      <div class="item">
        <h4>Value 8 </h4><canvas id="my_canvas8" width="170" height="170"></canvas></div>
    </div>
  </div>
  <!--OWL JS-->
  <script src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js"></script>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

设置为循环时,猫头鹰轮播中的项目将被克隆,最终将导致多个id="my_canvas1"会产生冲突,但是更重要的是,如果您在DOM中甚至不存在克隆的元素正在调用您的函数progressSim(id,percent)

如果您有我可以编辑的Codepen或jsfiddle示例,我可以仔细看看。

答案 1 :(得分:1)

请在以下步骤中找到带有画布项目的猫头鹰传送带的工作示例

  1. 猫头鹰传送带创建上一个/下一个项目的克隆以允许组循环(例如,比单击导航球要多)。结果,您将看到项目的克隆,但是在简单克隆canvas元素期间,将不会克隆图像。

  2. 因此,您需要为每个克隆的元素初始化画布(调用progressSim()),但是例如插件不允许您仅获取克隆的元素,则可以按类{{1}进行过滤}或对已初始化的项目使用标记(我更喜欢第二种方式)。

  3. 您为轮播的每个项目设置一个“ id” 属性,这是个坏主意,因为您可以获得相同cloned的副本在DOM中(执行克隆操作)。

  4. 最好对所有项目使用相同的类,并只调用一次id,而不是分别对所有项目进行调用,因为项目数量可以动态变化。

  

example of owl carousel with canvas的提琴手
  看看 JS代码中的一些注释部分

Github issue connected with this question.