简体   繁体   English

显示x + y坐标定义的图像部分的最佳方法?

[英]Best way to display part of image defined by x+y coordinates?

So, I have a load of database entries, each with an associated image file, and X and Y coordinates indicating which particular part of the image file it relates to. 因此,我有大量的数据库条目,每个条目都有一个关联的图像文件,以及X和Y坐标指示它与图像文件的哪个特定部分有关。 See the image and x1/y1/x2/y2 columns below 请参阅下面的图像和x1 / y1 / x2 / y2列

|  idx | code   | ref        | imagesub | image      | x1   | y1   | x2   | y2   |
-------+--------+------------+----------+------------+------+------+------+------+
| 5997 | MDX    | 1,1        | 1        | 02.png     |  38  |  216 |  717 |  436 |
| 5998 | MDX    | 1,2        | 1        | 02.png     |  38  |  375 |  720 |  478 |
| 5999 | MDX    | 1,3        | 1        | 02.png     |  38  |  448 |  709 |  597 |

I have a Django page for each of the entries, and I'd like to display only the relevant bit of the image - eg for entry 5997, I'd like to display just part of 02.png: (38,216) to (717,436). 我为每个条目都有一个Django页面,并且我只想显示图像的相关位-例如,对于条目5997,我只想显示02.png的一部分:(38,216)至(717,436) )。

What's the best way to do this? 最好的方法是什么? Is there a smart way to do it using some kind of JavaScript library? 是否有使用某种JavaScript库的聪明方法?

Or should I write a script to chop up the images myself first, then rename them, and display the smaller images? 还是我应该编写一个脚本来自己先切碎图像,然后重命名它们并显示较小的图像?

An alternative would be to display the entire image, but put some kind of highlight or frame around the relevant bits - if this is possible using JQuery or something similar? 一种替代方法是显示整个图像,但在相关位周围放置某种高光或框架-如果可以使用JQuery或类似方法实现呢?

Basically I can do whatever seems like the most sensible technical solution. 基本上,我可以做任何看起来最明智的技术解决方案。 I suppose it would be nice to show people a smaller image first (to reduce page loading time), then have the option of seeing the larger image with a frame around it. 我想最好先给人们看一个较小的图像(以减少页面加载时间),然后选择查看带有框架的较大图像。

So: 所以:

  1. is it possible to display just part of an image using JS: and 是否可以使用JS仅显示图像的一部分:
  2. is it possible to highlight part of an image, also with JS? 是否可以用JS突出显示图像的一部分?

EDIT: 编辑:

the jQuery Image Annotation Plugin looks good for Q2, maybe? jQuery图像注释插件在第二季度看起来不错,也许吗?

Actually there's a fairly easy way to do this, don't think in terms of <img> , think instead of background or background-image ( the CSS properties ). 实际上,有一个相当简单的方法可以做到这一点,不要以<img>思考,而不要以backgroundbackground-imageCSS属性 )来思考。

I can't say exactly what your code looks like without knowing your platform, but if say you had the properties available in the page, I can show you the jQuery, like this: 在不了解平台的情况下,我无法确切地说出代码的外观,但是如果您说页面中有可用的属性,那么我可以向您展示jQuery,如下所示:

//for (38,216) to (717,436)
var img = { x1: 38, x2: 717, y1: 216, y2: 436, url: "02.png" };

When you can just display the image in say a <div id="myImage"></div> like this: 当您只可以显示图像时,说<div id="myImage"></div>像这样:

$("#myDiv").css({
  width: img.x2 - img.x1,
  height: img.y2 - img.y1,
  background: "url('" + img.url + "')",
  backgroundPosition: -img.x1 + "px " + -img.y1 + "px"
});

You can view a demonstration here , another side benefit of this is that the user (for your example) would load 02.png only once, but use it multiple times, from cache. 您可以在此处查看演示 ,这样做的另一个好处是,用户(例如,您)仅从缓存中加载一次02.png ,但是多次使用它。 For more info the term that describe this/commonly used is CSS sprites , there are lots of articles out there around this . 有关更多信息,描述此/常用的术语是CSS sprites围绕此很多文章

If it doesn't need to be dynamic, you can of course render all of these properties directly out in the style attribute of the <div> directly, I'm just not exactly sure where the work has to be done (client vs server) here. 如果不需要动态,当然可以直接在<div>style属性中直接渲染所有这些属性,我不确定是否必须在哪里完成工作(客户端与服务器) ) 这里。

Probably best to cut up the images server side regardless. 无论如何,最好分割图像服务器端。 For one it is the only way to reduce loading time, and it sidesteps any sort of browser compatibility or disabled JavaScript issues. 其中一个是减少加载时间的唯一方法,它回避了任何类型的浏览器兼容性或禁用的JavaScript问题。 Also, it is relatively easy especially if you can use Imagemagick, and if you need to do any resizing of the image, the quality will be better and consistent. 而且,这相对容易,尤其是如果可以使用Imagemagick,并且如果需要对图像进行任何大小调整,则质量会更好且保持一致。

But as to your questions, yes and yes. 但是关于您的问题,是的,是的。

In fact you do not really need JavaScript at all, CSS should do the job (though I guess that would be true of nearly all page layout manipulations...). 实际上,您根本不需要JavaScript,CSS应该可以完成这项工作(尽管我猜想几乎所有页面布局操作都是如此……)。 The key is not to use the <img> element, as resizing that also scales and distorts the image attached to it. 关键是不要使用<img>元素,因为调整大小还会缩放和扭曲附加到它的图像。 Use any generic element, even <a> if you want the image to be clickable: 如果您希望图像可点击,请使用任何通用元素,甚至使用<a>

a#image {
  display: block;
  background-image: url(someimage.png);
  background-position: 100px 250px;
  width: 500px;
  height: 700px;
}

Pretty simple stuff, should not have much issue with browser compatibility (though don't quote me on that). 很简单的东西,浏览器兼容性应该不会有太大问题(尽管不要在此引用我)。

If you want an area of the image highlighted you could put a <span> element inside the image containing element and style it like: 如果要突出显示图像的某个区域,可以将<span>元素放在包含该元素的图像内,并设置其样式:

a#image span {
  position: absolute;
  top: 30px;
  left: 50px;
  width: 10px;
  height: 10px;
  background-color: orange;
  opacity: .7;
  display: block;
}

/* And make sure the containing image element has the right 'position' */
a#image { position: relative; }

All this is off the top of my head so you might need to do some experimenting, but the idea is there. 所有这些都不在我的脑海中,所以您可能需要做一些实验,但是这个想法就在那里。

If you do not need to resize the image or save bandwidth, and you expect users to often view the full size image, setting such styles with JavaScript might be beneficial. 如果您不需要调整图像大小或节省带宽,并且希望用户经常查看完整尺寸的图像,则使用JavaScript设置此类样式可能会有所帮助。 Still, for most cases, server-side image manipulation is the way to go. 尽管如此,在大多数情况下,服务器端图像处理还是可行的方法。

The best way to do this is definitely CSS via technique called CSS sprites (basically the same thing MooGoo is advocating). 最好的方法肯定是通过称为CSS精灵的技术来实现CSS (基本上MooGoo提倡的是同一件事)。 There's no sense in resorting to javascript when the CSS solution will be much more widely supported by browsers. 当CSS解决方案将被浏览器更广泛地支持时,没有任何理由使用javascript。

The only reasons to actually split up the image on the server would be if your image was extremely large and the individual bits you wanted to display were small - otherwise, you'll actually see a performance benefit from serving the same image, as it'll be cached in the client's browser after the initial request. 在服务器上实际拆分映像的唯一原因是,如果映像非常大且要显示的单个位很小-否则,您实际上会从提供相同映像中看到性能优势,因为初始请求后,它将被缓存在客户端的浏览器中。

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

相关问题 与x或x + y匹配的简单正则表达式 - Simple regex that matches x or x+y 通过指向X,Y坐标移动图像 - move image by pointing to the X, Y coordinates 使用 javascript 在图像上绘制 x 和 y 坐标 - Plot x and y coordinates on an image using javascript 使用Javascript中的X,Y坐标裁剪图像 - Crop image with X, Y coordinates in Javascript 当用户将鼠标悬停在图像上时,如何向用户实时显示图像上的(x,y)坐标? - How could I display (x,y) coordinates on image in real-time to the user when the user hovers his mouse over the image? 使用JQuery将带有x和y坐标的图标放置在另一幅图像的顶部 - Position icons with x and y coordinates on top of another image using JQuery HTML5定义图像各部分坐标的最佳方法,当有人单击该坐标时,我们可以找到单击了哪个部分 - HTML5 Best way to define coordinates for parts of an image, that when someone clicks on that we can find which part clicked 如何在javascript中找到图像点的x和y坐标 - how to find the x and y coordinates of the image points in javascript 如何围绕xy坐标动态裁剪图像? - how to dynamically crop an image around x-y coordinates? 如何在 html 中获取图像上项目的 plot x、y 坐标 - How to plot x,y coordinates of item on a image in html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM