A Pen by Edward Lance Lorilla on CodePen.
Created
June 5, 2021 16:03
-
-
Save edwardlorilla/3e2d6a0634a658d71097741a8aa79c90 to your computer and use it in GitHub Desktop.
water marker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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