简体   繁体   English

HTML画布元素创建不希望的白色边框

[英]HTML canvas element creating undesired white border

There's probably a real simple explanation for this, but I'm struggling really hard to find it. 对此可能有一个真正简单的解释,但是我很难找到它。 I'm trying to implement a crop picture feature and I'm getting an undesired border on the right and bottom sides of the picture, as seen here: http://imgur.com/Xjtk39K . 我正在尝试实现裁剪图片功能,并且在图片的右侧和底部都得到了不想要的边框,如此处所示: http : //imgur.com/Xjtk39K I think I've narrowed it down to something having to do with the first image translation that happens when I crop the picture. 我想我将其范围缩小到与裁剪图片时发生的第一次图像转换有关。 Here's the applicable code (the doubling of the cropped image size in submitPhotoUpload is due to some unknown halving of the picture height in my code. Advice for that would be welcome too): 这是适用的代码( submitPhotoUpload裁剪的图像大小submitPhotoUpload是由于我的代码中的图片高度减半而引起的,对此的建议也将受到欢迎):

cropPhoto: function(evt){

    var formType = App.UploadPhotoView.whichForm;

    var canvas = document.getElementById("crop-canvas-preview-"+formType);
    var context = canvas.getContext("2d");
    if(this.canvasHidden){
        canvas.style.display = 'inline';
        App.UploadPhotoView.canvasHidden = false;
    }

    var x1 = App.UploadPhotoView.x1;
    var y1 = App.UploadPhotoView.y1;
    var width = App.UploadPhotoView.width;

    var previewSize = $("#upload-div-"+formType).height() * 0.25;
    var resizeX = App.UploadPhotoView.scalingFactorX;
    var resizeY = App.UploadPhotoView.scalingFactorY;
    var img = document.getElementById("preview-photo-"+formType);

    context.clearRect(0, 0, previewSize, previewSize);
    context.drawImage(img, x1*resizeX, y1*resizeY, width*resizeX,
                      width*resizeY, 0, 0, previewSize*2, previewSize);
},

submitPhotoUpload: function(){
    var formType = "pic";
    if ($('#preview-photo-'+formType).attr('src') != '#') {
        var uploadImage = new Image();
        uploadImage.src = document.getElementById('crop-canvas-preview-'+formType).toDataURL();

        var canvas = document.createElement('canvas');
        canvas.width = uploadImage.width * 3;
        canvas.height = uploadImage.height * 6;

        canvas.getContext('2d').drawImage(uploadImage, 0, 0, canvas.width, canvas.height);

        var newPhoto = App.Photo.createRecord({
            filename: canvas.toDataURL(),
            location: this.get('location'),
            description: this.get('description')
        });
        this.get('store').commit();
        resetPhotoUpload(formType);
    }else{
        this.set('submissionFailed', true);
    }
},

The reason is that you are using fractional values with the clearing and clipping. 原因是您正在使用分数值进行清除和剪切。

The clearRect call will clear on sub-pixel basis which means it can leave "half" pixel clipped. clearRect调用将在子像素的基础上清除,这意味着可以保留“一半”的像素。

The clip however cannot clip the image with sub-pixels as images are pixel based and will truncate the value to match an integer value, hence you get a small gap in the final result. 但是,由于图像是基于像素的,因此剪辑无法使用子像素来剪辑图像,并且将截断该值以匹配整数值,因此最终结果会出现很小的差距。

Luckily this is easy to get around by simply forcing the values to integer values: 幸运的是,只需将值强制为整数即可轻松解决:

ONLINE DEMO HERE 在线演示

context.clearRect(0, 0, previewSize|0, previewSize|0);
context.drawImage(img, (x1*resizeX)|0, (y1*resizeY)|0, (width*resizeX)|0,
                  (width*resizeY)|0, 0, 0, previewSize|0, previewSize|0);

Of course, you might consider doing this outside the function call. 当然,您可以考虑在函数调用之外执行此操作。 If you need a more accurate rounding just add 0.5 to the values before doing as here, shifting 0 bits (which force a float number to integer) or use Math.round() instead. 如果需要更精确的舍入,只需在此之前在值上加上0.5 ,然后将0位移位(这将浮点数强制为整数)或改用Math.round()

Here is the result: 结果如下:

结果

left: cleared and clipped using frational positions, right: using integer values 左:使用分形位置清除和剪切,右:使用整数值

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

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