Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the watermarking effect with vue

Shulou Source: shulou.com Published: 2022-06-03 19:23:08 10月05日 Update

This article is about how vue achieves the effect of watermarking. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Foreword:

Watermark effect is used in vue project. You can specify a container.

Effect picture:

1. Do not specify a container

2. Specify the container

Implementation method:

1. Create a new configuration file watermark.js, which can be placed in util or somewhere else.

Let watermark = {} let setWatermark = (text SourceBody) = > {let id = Math.random () * 10000+'-'+Math.random () * 10000+'/'+Math.random () * 10000 if (document.getElementById (id)! = = null) {document.body.removeChild (document.getElementById (id))} let can = document.createElement ('canvas') can.width = 150can.height = can.getContext (' 2d') cans.rotate (- 20 * Math.PI / cans) cans. Font = '15px Vedana' cans.fillStyle =' rgba (0 0, 0, .5) 'cans.textAlign =' left' cans.textBaseline = 'Middle' cans.fillText (text, can.width / 20 Can.height) let water_div = document.createElement ('div') water_div.id = id water_div.style.pointerEvents =' none' water_div.style.background = 'url (' + can.toDataURL ('image/png') +') left top repeat' if (sourceBody) {water_div.style.width = '100%' water_div.style.height = '100%' sourceBody.appendChild (water_div)} else {water_ Div.style.top = '3px' water_div.style.left =' 0px' water_div.style.position = 'fixed' water_div.style.zIndex =' 100000' water_div.style.width = document.documentElement.clientWidth + 'px' water_div.style.height = document.documentElement.clientHeight +' px' document.body.appendChild (water_div)} return id} / * * this method allows only one call Secondary * @ param: * @ text = = watermark content * @ sourceBody = = where is the watermark added Body * * / watermark.set = (text, sourceBody) = > {let id = setWatermark (text, sourceBody) setInterval () = > {if (document.getElementById (id) = null) {id = setWatermark (text, sourceBody)}}, 2000) _ window.onresize = () = > {setWatermark (text, sourceBody)} export default watermark

2. Configure globally in main.js

/ / Watermark import watermark from'. / utils/watermark.js'Vue.prototype.$watermark = watermark

3. Use-full screen watermark in the page

This.$watermark.set ("Haoxing 2731")

4. Use in the page-specify a container

Click on me to add watermark addWatermark () {this.$watermark.set ("Haoxing 2731", this.$refs.content)}

5. If you think the distance between fonts is too large, just change this property.

Can.width = 150can.height = 120Thank you for your reading! This is the end of this article on "how to achieve watermarking effect in vue". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Tags: Watermark effect container content method more article page Haoxing configuration good practical between global preface place font that is attributes rendering Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Apple MariaDB Linux Shulou Information