删除画布平滑以重新投影静态图像层

需要重新投影的静态图像层不允许禁用图像平滑。

这是我正在使用的图层:

var layer = new ol.layer.Image({
    source: new ol.source.ImageStatic({
        url: "grid.png",imageExtent: [-130,20,-55,50],projection: 'EPSG:4326'
    })
});

放大时,像素边缘模糊(锯齿),需要清晰。

以下是无效的,因为prerender钩子是在创建画布的ol / reproj.js内的投影工作之后出现的:

layer.on('prerender',function(evt) {
      evt.context.imageSmoothingEnabled = false;
});

这是我与此同时使用的hack:

diff --git a/src/ol/reproj.js b/src/ol/reproj.js
index 7593c6b..5e0d090 100644
--- a/src/ol/reproj.js
+++ b/src/ol/reproj.js
@@ -103,6 +103,11 @@ export function render(width,height,pixelRatio,context.scale(pixelRatio,pixelRatio);

+  context.imageSmoothingEnabled = false;
+  context.webkitImageSmoothingEnabled = false;
+  context.mozImageSmoothingEnabled = false;
+  context.msImageSmoothingEnabled = false;
+
   const sourceDataExtent = createEmpty();
   sources.forEach(function(src,i,arr) {
     extend(sourceDataExtent,src.extent);

我错过了什么吗?有没有一种方法而无需重建openlayers?

shuizainuli 回答:删除画布平滑以重新投影静态图像层

基于How can I override a native JS function ( canvas's getContext() ) in pure JavaScript?

您可以禁用 所有 2d画布上下文的平滑处理

// store a reference to original vector
HTMLCanvasElement.prototype.__oldGetContext = HTMLCanvasElement.prototype.getContext;

// patch
HTMLCanvasElement.prototype.getContext = function(type,options) {
  var context = this.__oldGetContext(type,options);   // call original vector
  if (type === "2d") {
    context.imageSmoothingEnabled = false;
    context.webkitImageSmoothingEnabled = false;
    context.mozImageSmoothingEnabled = false;
    context.msImageSmoothingEnabled = false;
  }
  return context;
}
本文链接:https://www.f2er.com/3135808.html

大家都在问