Is it possible to apply CSS to half of a character?

后端 未结 19 1894
轻奢々
轻奢々 2020-11-22 16:46

What I am looking for:

A way to style one HALF of a character. (In this case, half the letter being transparent)

Wh

19条回答
  •  臣服心动
    2020-11-22 17:16

    Here an ugly implementation in canvas. I tried this solution, but the results are worse than I expected, so here it is anyway.

    Canvas example

    $("div").each(function() {
      var CHARS = $(this).text().split('');
      $(this).html("");
      $.each(CHARS, function(index, char) {
        var canvas = $("")
          .css("width", "40px")
          .css("height", "40px")
          .get(0);
        $("div").append(canvas);
        var ctx = canvas.getContext("2d");
        var gradient = ctx.createLinearGradient(0, 0, 130, 0);
        gradient.addColorStop("0", "blue");
        gradient.addColorStop("0.5", "blue");
        gradient.addColorStop("0.51", "red");
        gradient.addColorStop("1.0", "red");
        ctx.font = '130pt Calibri';
        ctx.fillStyle = gradient;
        ctx.fillText(char, 10, 130);
      });
    });
    
    
    Example Text

提交回复
热议问题