简体   繁体   English

Tumblr和HTML5-方格的画布?

[英]Tumblr and HTML5 - Canvas for Square Grid?

I am trying to add HTML5 to a tumblr theme to generate 250px by 250px crops of the image posts found on a blog and use them in place of the normal 500px image. 我正在尝试将HTML5添加到tumblr主题中,以生成在博客上找到的图片帖子的250px x 250px的裁剪,并使用它们代替正常的500px图片。 I am currently using the following code to load the images within the photo posts: 我目前正在使用以下代码在照片文章中加载图像:

  <canvas id="myCanvas" width="250" height="250"></canvas>
  <script>
  var canvas = document.getElementById('myCanvas');
  var context = canvas.getContext('2d');
  var imageObj = new Image();

  imageObj.onload = function() {
    // draw cropped image
    var sourceX = 150;
    var sourceY = 0;
    var sourceWidth = 250;
    var sourceHeight = 250;
    var destWidth = sourceWidth;
    var destHeight = sourceHeight;
    var destX = canvas.width / 2 - destWidth / 2;
    var destY = canvas.height / 2 - destHeight / 2;

    context.drawImage(imageObj, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight);
  };
  imageObj.src = '{PhotoURL-500}';
  </script>

With the above code, I am given a 250x250 crop of the last photo that would be loaded on the page in the position of the first photo post which can be seen here . 使用上面的代码,我得到了最后一张照片的250x250裁剪图,该图片将被加载到页面上第一张照片发布的位置,可以在此处看到。 I've done some research and from what I understand, the issue lies within imageObj.onload but I am not sure what alterations are necessary to make the code properly display the photos. 我已经进行了一些研究,据我了解,问题出在imageObj.onload之内,但我不确定为使代码正确显示照片需要进行哪些更改。

Any ideas of what I can do to fix this? 关于该如何解决的任何想法?

All of your canvas id's are the same, which is a no no, and explains why the only canvas with anything drawn to it is the first one. 您所有的画布ID都是相同的,这是一个否定的原因,并解释了为什么唯一带有任何内容的画布是第一个。 I'm not really sure why you have so much repeating code (multiple script blocks which are completely identical aside from the image url), but as long as you ensure each canvas has a unique id referenced appropriately in its corresponding script block as well as unique Image objects, it should work. 我不太确定为什么会有这么多的重复代码(除了图像URL之外,多个脚本块完全相同),但是只要您确保每个画布在其相应的脚本块以及唯一的Image对象,它应该可以工作。

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

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