简体   繁体   English

使用图像而不是画布

[英]Using Image instead of Canvas

I have a Image and I want to paint on top of it some square. 我有一张图片,我想在上面画一些正方形。 But I keep getting undefined function on getContext('2d') . 但是我一直在getContext('2d')上获得未定义的函数。 I must add that the var img is an image which is already loaded on the page, I'm trying to interpret it as a canvas since I am using another script which lets to select areas on the image and if I use a canvas the script will not work. 我必须补充一点,var img是已经加载到页面上的图像,由于我正在使用另一个脚本来选择图像上的区域,因此我试图将其解释为画布,如果使用画布,则该脚本不管用。 So in the case where I can not interpret a image as a canvas what would you suggest? 因此,在我无法将图像解释为画布的情况下,您会提出什么建议?

Js function JS功能

function drawInput(dx1,dy1,dx2,dy2) {
    var img = document.getElementById('home:tempImg');

    var canvas = img;
    console.log(canvas);
    var context = canvas.getContext('2d');
    var imageObj = new Image();

    imageObj.onload = function() {
      context.drawImage(imageObj, dx1, dy1,dx2-dx1,dy2-dy1);
    };
    imageObj.src = 'images/selected.png';
}

HTML HTML

<h:body>


    <h:form onsubmit="#{getComponents.getAllComponents()}" id="home">

        <div>

            <p:graphicImage id="tempImg" rendered="true" value="#{imageView.selectedImg}">
            </p:graphicImage>

        </div>
</h:form>
</h:body>
    <script type="text/javascript">
        var myJSONStr ='{ "area" : #{areaInputView.areaListString}}';

        console.log(myJSONStr);


        var json = JSON.parse(myJSONStr);
        //console.log(json.area[1].x1);
        for(var i=0;myJSONStr.length >i ; i++) {
            console.log(json.area[i].x1, json.area[i].y1, json.area[i].x2, json.area[i].y2);
            drawInput(json.area[i].x1, json.area[i].y1, json.area[i].x2, json.area[i].y2);
        };
    </script>
</html>

By leveraging custom JS 'classes' (of the type app.Image in this case), we can build a pretty neat wrapper that allows us to draw images dynamically based on other instances we have kept track of. 通过利用自定义的JS“类”(在这种情况下为app.Image类型),我们可以构建一个漂亮的包装器,使我们可以根据跟踪的其他实例动态绘制图像。

Check out this Codepen for a working example. 查看此Codepen中的示例。

window.app = window.app || {};
app.Canvas = document.getElementById('myCanvas').getContext('2d');
app.Image = function(source, x, y) {
  this.x = x;
  this.y = y;
  this.data = new Image();
  this.data.addEventListener('load', this.draw.bind(this));
  this.data.src = source;
};

app.Image.prototype.draw = function() {
    app.Canvas.drawImage(this.data, this.x, this.y);
};

app.Image.prototype.getImageBounds = function() {
    return {
      height: this.data.height,
      width: this.data.width
    }
};

var myImage = new app.Image('http://placehold.it/250x250', 10, 10);
var mySecondImage = new app.Image('http://placesheen.com/100/100', myImage.x + 10, myImage.y + 10);

The most important line is the last one. 最重要的一行是最后一行。 Notice how we're initializing a new app.Image with x and y coordinates relative to the first image. 注意我们如何初始化一个新的app.Image 对于第一个图像具有xy坐标。 By using a custom wrapper class that keeps track of this information, we're able to easily access the necessary data that we need in a variety of circumstances. 通过使用跟踪该信息的自定义包装器类,我们可以轻松地访问在各种情况下所需的必要数据。

I hope this helps to get you on the right track! 我希望这有助于您走上正确的道路! Let me know if you have any questions about the code, and I'll be glad to explain. 如果您对代码有任何疑问,请告诉我,我们将很乐于解释。

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

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