简体   繁体   English

以文本为掩码的 CSS div

[英]CSS div with text as mask

I am trying to achieve a mask-type effect with a coloured div's large text in HTML.我正在尝试使用 HTML 中的彩色 div 大文本实现蒙版类型的效果。 I want this masked text to show parts of an image which is behind the div.我希望此蒙版文本显示 div 后面的图像部分。 However;然而; instead of using the background-clip property and a background-image on the div itself - I am hoping to simply reveal what ever is underneath the element (in my case, the image).而不是在 div 本身上使用background-clip属性和background-image - 我希望简单地揭示元素下面的内容(在我的情况下,图像)。 I have tried using svg images with compound paths, they proved too difficult to handle.我曾尝试使用带有复合路径的 svg 图像,但事实证明它们太难处理了。 Is there ANY other way I could do this?我还有其他方法可以做到这一点吗? CSS? CSS? jQuery plugin? jQuery插件?svg 的一次不太成功的尝试

Based on this article for applying a text mask , you could do this with a canvas tag.根据这篇关于应用文本掩码的文章,您可以使用画布标签来完成此操作。

<div id="bg"><canvas id="overlay" width="240" height="70"></canvas></div>
<script>
// Get a handle to our canvas
var ctx = document.getElementById('overlay').getContext("2d");

// Choose font
ctx.font = "Bold 36px 'Helvetica'";

// Draw black rectangle
ctx.fillStyle = "black"; 
ctx.fillRect(0,0,230,70); 

// Punch out the text!
ctx.globalCompositeOperation = 'destination-out'; 
ctx.fillText("Some Text", 25, 50);
</script>

http://jsfiddle.net/6aVGU/ http://jsfiddle.net/6aVGU/

You could do it using the Canvas object: http://jsfiddle.net/JackKalish/g3BDa/3/你可以使用 Canvas 对象做到这一点: http : //jsfiddle.net/JackKalish/g3BDa/3/

    var canvas1 = document.getElementById("canvas1");
    var canvasContext1 = canvas1.getContext("2d");

    // destination-out 
    // Text cuts-out everything under it
    // background is revealed in the cut-out
    makeGradientAndFont(canvasContext1, canvas1);
    canvasContext1.globalCompositeOperation = 'destination-out';
    canvasContext1.fillText("Portfolio", 175, 50);

    function makeGradientAndFont(ctx, canvas) {
        var grad = ctx.createLinearGradient(0, 0, canvas.width, canvas.height);
        grad.addColorStop(0, '#0000FF');
        grad.addColorStop(.3, '#00FFFF');
        grad.addColorStop(.4, '#99FFFF');
        grad.addColorStop(.5, '#00FF00');
        grad.addColorStop(.6, '#FFFF00');
        grad.addColorStop(.8, '#F00000');
        ctx.rect(0, 0, canvas.width, canvas.height);
        ctx.fillStyle = grad;
        ctx.fill();
        ctx.fillStyle = "black";
        ctx.font = "55px Arial";
        ctx.textAlign = "center";
        ctx.textBaseline = "middle";
    }

您可以使用文本的透明 PNG 图像来执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM