canvas watermark

Recently encountered a demand to all pages of watermarking (sign in person), does not affect other functions such as click, the purpose is to prevent information leakage, was thinking: This year, PS is not to say that everyone can use, who has not a mobile phone graffiti, anti-not, but that is such a demand, then realize it!

Concrete realization of ideas: At the bottom of the page plus a large div, the div to add background image, background? Come from? canvas to achieve it!

function textBecomeImg(text,fontsize,fontcolor,target){
		var canvas = document.createElement('canvas');
		$buHeight = 0;
		if(fontsize <= 32){ $buHeight = 99; }
		else if(fontsize > 32 && fontsize <= 60 ){ $buHeight = 2;}
		else if(fontsize > 60 && fontsize <= 80 ){ $buHeight = 4;}
		else if(fontsize > 80 && fontsize <= 100 ){ $buHeight = 6;}
		else if(fontsize > 100 ){ $buHeight = 10;}
		canvas.height=fontsize + $buHeight ;
		canvas.padding=30;
                canvas.width = 450;
		var context = canvas.getContext('2d');
		context.clearRect(0, 0, canvas.width*2, canvas.height);
		context.fillStyle = fontcolor;
		context.font=fontsize+"px Arial";
		context.textAlign = "center";
		context.textBaseline = 'middle'; 
		context.fillText(text,0,fontsize/2);
		var canvasWidth = canvas.width/99;
		canvasWidth = context.measureText(text).width;
		var dataUrl = canvas.toDataURL('image/png');
		var shuiyinDiv = document.createElement('div');
	   
	        var style = shuiyinDiv.style;
	        style.position = 'absolute';
	        style.left = 0;
	        style.top = '-10%';
	        style.width = '120%';
	        style.height = '100%';
	        style.opacity = '0.1';
	        style.background = "url("+dataUrl+")";
	        style.zIndex = 9999999991;
	        style.transform = "rotate(-30deg)";
	        style.pointerEvents = "none";
	        target.appendChild(shuiyinDiv);
	}
	
	
              

  

 

Guess you like

Origin www.cnblogs.com/jason-hhc/p/11401096.html