简体   繁体   English

HTML5中的多个动态画布

[英]Multiple Dynamic Canvases in HTML5

For a website I'm making I'm going to need multiple canvases that make a ring around individual images. 对于一个我正在制作的网站,我将需要多个围绕单个图像环绕的画布。

I've experimented with HTML, CSS, and JS in JSfiddle and have managed to create the desired effect I want with a single image, but when I duplicate the html it doesn't make a second canvas (or maybe it's making another, just the same place as the first?) 我已经在JSfiddle中对HTML,CSS和JS进行了实验,并且设法用单个图像创建了想要的效果,但是当我复制html时,它并不能构成第二个画布(或者可能是在形成另一个画布,只是与第一个相同的地方?)

https://jsfiddle.net/impo/87e8yqnt/23/ https://jsfiddle.net/impo/87e8yqnt/23/

Above is my jsfiddle, how would I change it so that every image is with its canvas? 上面是我的jsfiddle,我将如何更改它,以便每个图像都带有其画布?

  var canvas = document.getElementById('border'); var context = canvas.getContext('2d'); var x = 58; var y = 58; var radius = 55; var startAngle = 1.5 * Math.PI; var endAngle = 1 * Math.PI; var counterClockwise = false; context.beginPath(); context.arc(x, y, radius, 0, 2 * Math.PI, false); context.lineWidth = 3; // line color context.strokeStyle = '#ebebeb'; context.stroke(); context.beginPath(); context.arc(x, y, radius, startAngle, endAngle, counterClockwise); context.lineWidth = 4; context.strokeStyle = '#8aff92'; context.stroke(); 
 .pkmn { position: relative; display: inline-block; margin: 5px; width: 116px; height: 116px; } .pkmn .sprite { padding: 5px; position: relative; top: 5px; left: 5px; z-index: -1; } .pkmn .lvl { font-family: 'Courier New Regular', sans-serif; font-size: 30px; z-index: 100; position: absolute; margin-top:0; } .pkmn .crown { position: absolute; left: 44px; bottom: 9px; } .pkmn .item { position: absolute; left: 10px; bottom: 12px; } .pkmn .star { position: absolute; right: 30px; top: 14px; } .pkmn .heart { position: absolute; right: 10px; top: 40px; } .pkmn .exp { position: absolute; display: inline-block; } 
 <div class="pkmn"> <canvas id="border" class="exp" width="115" height="115"></canvas> <div class="lvl">95</div> <img alt="test" src="http://cdn.bulbagarden.net/upload/7/76/Spr_5b_143_s.png" class="sprite"> <img alt="item" src="http://cdn.bulbagarden.net/upload/0/0f/Bag_Leftovers_Sprite.png" class="item"> <img alt="crown" src="http://cdn.bulbagarden.net/upload/c/c5/Leaf_Crown_Sprite.png" class="crown"> <img alt="star" src="http://cdn.bulbagarden.net/upload/2/27/ShinyVIStar.png" class="star"> <img alt="heart" src="http://i1055.photobucket.com/albums/s519/impojr/heart_zpsdaihll9m.png" class="heart"> </div> <div class="pkmn"> <canvas id="border" class="exp" width="115" height="115"></canvas> <div class="lvl">95</div> <img alt="test" src="http://cdn.bulbagarden.net/upload/7/76/Spr_5b_143_s.png" class="sprite"> <img alt="item" src="http://cdn.bulbagarden.net/upload/0/0f/Bag_Leftovers_Sprite.png" class="item"> <img alt="crown" src="http://cdn.bulbagarden.net/upload/c/c5/Leaf_Crown_Sprite.png" class="crown"> <img alt="star" src="http://cdn.bulbagarden.net/upload/2/27/ShinyVIStar.png" class="star"> <img alt="heart" src="http://i1055.photobucket.com/albums/s519/impojr/heart_zpsdaihll9m.png" class="heart"> </div> 

Warning Quirks Mode. 警告怪癖模式。

Don`t use duplicated ID's this will cause your page to slow down and many behaviors to change due to the fact the browser switches to quirks mode . 不要使用重复的ID,这会导致您的页面速度变慢,并且由于浏览器切换到“ 怪癖”模式 ,许多行为都会改变。

I add this answer for that reason and because the given answer has another very bad bit of code. 由于这个原因,我添加了这个答案,因为给定的答案还有另一段非常糟糕的代码。

for (var i = 0; i < document.querySelectorAll('.border').length; i++) {  
      var canvas = document.querySelectorAll('#border')[i];

This code does 2 document queries for every item found by the query. 此代码对查询找到的每个项目进行2个文档查询。 This would noticeably slow the page with just a few dozen matching elements on low end machines. 在低端机器上,只有几十个匹配元素,这会明显减慢页面速度。

 const canvases = document.querySelectorAll('.exp') for (var i = 0; i < canvases.length; i++) { var canvas = canvases[i]; var context = canvas.getContext('2d'); var x = 58; var y = 58; var radius = 55; var startAngle = 1.5 * Math.PI; var endAngle = 1 * Math.PI; var counterClockwise = false; context.beginPath(); context.arc(x, y, radius, 0, 2 * Math.PI, false); context.lineWidth = 3; // line color context.strokeStyle = '#ebebeb'; context.stroke(); context.beginPath(); context.arc(x, y, radius, startAngle, endAngle, counterClockwise); context.lineWidth = 4; context.strokeStyle = '#8aff92'; context.stroke(); } 
 .pkmn { position: relative; display: inline-block; margin: 5px; width: 116px; height: 116px; } .pkmn .sprite { padding: 5px; position: relative; top: 5px; left: 5px; z-index: -1; } .pkmn .lvl { font-family: 'Courier New Regular', sans-serif; font-size: 30px; z-index: 100; position: absolute; margin-top:0; } .pkmn .crown { position: absolute; left: 44px; bottom: 9px; } .pkmn .item { position: absolute; left: 10px; bottom: 12px; } .pkmn .star { position: absolute; right: 30px; top: 14px; } .pkmn .heart { position: absolute; right: 10px; top: 40px; } .pkmn .exp { position: absolute; display: inline-block; } 
 <div class="pkmn"> <canvas id="border1" class="exp" width="115" height="115"></canvas> <div class="lvl">95</div> <img alt="test" src="http://cdn.bulbagarden.net/upload/7/76/Spr_5b_143_s.png" class="sprite"> <img alt="item" src="http://cdn.bulbagarden.net/upload/0/0f/Bag_Leftovers_Sprite.png" class="item"> <img alt="crown" src="http://cdn.bulbagarden.net/upload/c/c5/Leaf_Crown_Sprite.png" class="crown"> <img alt="star" src="http://cdn.bulbagarden.net/upload/2/27/ShinyVIStar.png" class="star"> <img alt="heart" src="http://i1055.photobucket.com/albums/s519/impojr/heart_zpsdaihll9m.png" class="heart"> </div> <div class="pkmn"> <canvas id="border2" class="exp" width="115" height="115"></canvas> <div class="lvl">95</div> <img alt="test" src="http://cdn.bulbagarden.net/upload/7/76/Spr_5b_143_s.png" class="sprite"> <img alt="item" src="http://cdn.bulbagarden.net/upload/0/0f/Bag_Leftovers_Sprite.png" class="item"> <img alt="crown" src="http://cdn.bulbagarden.net/upload/c/c5/Leaf_Crown_Sprite.png" class="crown"> <img alt="star" src="http://cdn.bulbagarden.net/upload/2/27/ShinyVIStar.png" class="star"> <img alt="heart" src="http://i1055.photobucket.com/albums/s519/impojr/heart_zpsdaihll9m.png" class="heart"> </div> 

Wrap your code in for loop which goes through all the elements having id border. 将代码包装在for循环中,该循环遍历具有id边框的所有元素。 Below is modified code. 下面是修改后的代码。

 for (var i = 0; i < document.querySelectorAll('#border').length; i++) { var canvas = document.querySelectorAll('#border')[i]; var context = canvas.getContext('2d'); var x = 58; var y = 58; var radius = 55; var startAngle = 1.5 * Math.PI; var endAngle = 1 * Math.PI; var counterClockwise = false; context.beginPath(); context.arc(x, y, radius, 0, 2 * Math.PI, false); context.lineWidth = 3; // line color context.strokeStyle = '#ebebeb'; context.stroke(); context.beginPath(); context.arc(x, y, radius, startAngle, endAngle, counterClockwise); context.lineWidth = 4; context.strokeStyle = '#8aff92'; context.stroke(); } 
 .pkmn { position: relative; display: inline-block; margin: 5px; width: 116px; height: 116px; } .pkmn .sprite { padding: 5px; position: relative; top: 5px; left: 5px; z-index: -1; } .pkmn .lvl { font-family: 'Courier New Regular', sans-serif; font-size: 30px; z-index: 100; position: absolute; margin-top:0; } .pkmn .crown { position: absolute; left: 44px; bottom: 9px; } .pkmn .item { position: absolute; left: 10px; bottom: 12px; } .pkmn .star { position: absolute; right: 30px; top: 14px; } .pkmn .heart { position: absolute; right: 10px; top: 40px; } .pkmn .exp { position: absolute; display: inline-block; } 
 <div class="pkmn"> <canvas id="border" class="exp" width="115" height="115"></canvas> <div class="lvl">95</div> <img alt="test" src="http://cdn.bulbagarden.net/upload/7/76/Spr_5b_143_s.png" class="sprite"> <img alt="item" src="http://cdn.bulbagarden.net/upload/0/0f/Bag_Leftovers_Sprite.png" class="item"> <img alt="crown" src="http://cdn.bulbagarden.net/upload/c/c5/Leaf_Crown_Sprite.png" class="crown"> <img alt="star" src="http://cdn.bulbagarden.net/upload/2/27/ShinyVIStar.png" class="star"> <img alt="heart" src="http://i1055.photobucket.com/albums/s519/impojr/heart_zpsdaihll9m.png" class="heart"> </div> <div class="pkmn"> <canvas id="border" class="exp" width="115" height="115"></canvas> <div class="lvl">95</div> <img alt="test" src="http://cdn.bulbagarden.net/upload/7/76/Spr_5b_143_s.png" class="sprite"> <img alt="item" src="http://cdn.bulbagarden.net/upload/0/0f/Bag_Leftovers_Sprite.png" class="item"> <img alt="crown" src="http://cdn.bulbagarden.net/upload/c/c5/Leaf_Crown_Sprite.png" class="crown"> <img alt="star" src="http://cdn.bulbagarden.net/upload/2/27/ShinyVIStar.png" class="star"> <img alt="heart" src="http://i1055.photobucket.com/albums/s519/impojr/heart_zpsdaihll9m.png" class="heart"> </div> 

Use document.querySelectorAll() for getting all the elements having the id border. 使用document.querySelectorAll()获取具有ID边框的所有元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM