Skip to content

Instantly share code, notes, and snippets.

@edwardlorilla
Created June 5, 2021 16:03
Show Gist options
  • Save edwardlorilla/3e2d6a0634a658d71097741a8aa79c90 to your computer and use it in GitHub Desktop.
Save edwardlorilla/3e2d6a0634a658d71097741a8aa79c90 to your computer and use it in GitHub Desktop.
water marker
<div id="app">
<div class="water-marker" >
<div v-watermarker="{text:'lorem ipsum',textColor:'rgba(180, 180, 180, 0.4)'}">
<div class="water-marker-item">lorem ipsum</div>
</div>
</div>
</div>
function addWaterMarker(str, parentNode, font, textColor) {
// Watermark text, parent element, font, text color
var can = document.createElement('canvas')
parentNode.appendChild(can)
can.width = 200
can.height = 150
can.style.display ='none'
var cans = can.getContext('2d')
cans.rotate((-20 * Math.PI) / 180)
cans.font = font || '16px Microsoft JhengHei'
cans.fillStyle = textColor ||'rgba(180, 180, 180, 0.3)'
cans.textAlign ='left'
cans.textBaseline ='Middle'
cans.fillText(str, can.width / 10, can.height / 2)
parentNode.style.backgroundImage ='url(' + can.toDataURL('image/png') +')'
}
const waterMarker = {
bind: function (el, binding) {
addWaterMarker(binding.value.text, el, binding.value.font, binding.value.textColor)
},
}
new Vue({
directives: {
'watermarker':waterMarker
},
data(){
return{
}
},
methods:{
}
}).$mount('#app')
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.js"></script>
.water-marker{
height: 300px;
.water-marker-item{
line-height: 100vh;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment