canvas下的全屏问题

这段时间在管理后台,有用到图片全屏的,我是用canvas的引入的图片,哎!也是给自己挖了一个大坑,没办法,只能踩坑走啦,所以当时想着能不能直接用canvas直接进行图片全屏查看,不过网上百度出来的也是五花八门,结合百度自己整理了一个小的demo

HTML

<canvas id="blog" width="548" height="364"></canvas>
  <br/>
  <input value="点击全屏" onclick="fullScreen();" type="button">

JS

window.onload = function(){  
          //引入图片在canvas中
        var blog =document.getElementById('blog');  
        var context = blog.getContext('2d');  
        var img = new Image();  
        img.onload = function(){  
            draw(this);  
        };  
        img.src = 'img/2.jpg';  
        function draw(obj){  
            context.drawImage(obj,0,0);        
        }  
    };  
    //进行全屏并适配各浏览器
      function fullScreen() {
          var element = document.getElementById('blog'),method = "RequestFullScreen";
            var prefixMethod;
            ["webkit", "moz", "ms", "o", ""].forEach(function(prefix) {
                if (prefixMethod) 
                    return;
                if (prefix === "") {
                    // 无前缀,方法首字母小写
                    method = method.slice(0,1).toLowerCase() + method.slice(1);
                }
                var fsMethod = typeof element[prefix + method];
                    if (fsMethod + "" !== "undefined") {
                        // 如果是函数,则执行该函数
                        if (fsMethod === "function") {
                            prefixMethod = element[prefix + method]();
                        } else {
                            prefixMethod = element[prefix + method];
                        }
                    }
                }
            );
            return prefixMethod;
        };
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Canvas 全屏水印通常是指在 HTML5 的 `<canvas>` 元素上添加一个透明度较低的图像或文字,使其在整个页面背景中显示出来,即使canvas内容被放大或缩小时,水印也会保持始终可见。这样做的目的是为了保护版权或者提供品牌标识。 实现canvas全屏水印的方法主要包括以下几个步骤: 1. 创建 canvas 元素:首先在 HTML 中创建一个 `<canvas>` 标签,并设置其宽度和高度与浏览器视窗相同,使用 `requestAnimationFrame` 或 `window.onload` 来确保 canvas 加载完成。 ```html <canvas id="watermark-canvas"></canvas> ``` 2. 绘制水印:在 JavaScript 中,获取 canvas 的上下文 (`context`),然后在每一帧(动画循环)里绘制水印。你可以选择一个半透明的背景色(如 rgba(0,0,0,0.5))作为水印,然后在其上绘制文本或图片。 ```javascript const canvas = document.getElementById('watermark-canvas'); const ctx = canvas.getContext('2d'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; function drawWatermark() { ctx.fillStyle = 'rgba(0,0,0,0.5)'; ctx.fillRect(0, 0, canvas.width, canvas.height); // 绘制文字水印 const text = 'Your Watermark Text'; ctx.font = '16px Arial'; ctx.fillStyle = 'white'; ctx.fillText(text, 10, canvas.height - 20); } // 添加到动画循环 requestAnimationFrame(drawWatermark); ``` 3. 动态调整:如果你希望水印跟随窗口大小变化,你需要在 `drawWatermark` 函数中更新 canvas 的尺寸。 4. 隐藏canvas本身:为了完全实现全屏效果,可以设置 `canvas` 的 `display` 属性为 `none`,使实际的画布内容不显示,只留下水印。 ```javascript canvas.style.display = 'none'; ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值