KineticJS icon indicating copy to clipboard operation
KineticJS copied to clipboard

Special font issue and anti aliasing

Open ghost opened this issue 14 years ago • 8 comments

Hi,

I've try to add special fonts to my project by using CSS @font-face attribute. It working but when I change the font family by a select, at the first time the text does not appear, a the second change it appear ? Any idea ?

And is it possible to enable anti aliasing on text ?

Thx for help and thx for your amazing great work on this framework.

Best regards,

Clement

ghost avatar Nov 27 '12 10:11 ghost

I'm not seeing any issues with using custom non-standard fonts. The one I'm using shows up fine. One thing you may need to consider is that an @font-face font has to load a file in order to display, so if you're changing your font using a select but the font isn't loaded when you change it, it won't show up the way it should. I've found that adding a hidden or offscreen div with a single period (.) in it and a class that utilizes the font for each @font-face font I wish to include in the stage works quite well - it forces the DOM to load all of your font files on document.ready.

As for anti-aliasing, the fonts I've used so far show up perfectly. Are you sure the font you're using, and the browser you're using, should display the font crisper? Maybe do a side-by-side comparison of the text you're displaying - one in a div, the other on the canvas.

lazerblade01 avatar Nov 27 '12 15:11 lazerblade01

lazerblade01, thx to your answer.

You right for the font load, I need to 'precache' fonts before using in canvas. There is a lot of tool for that https://developers.google.com/webfonts/docs/webfont_loader?hl=fr

For the second thing, it does not work, I have inserted in a div the same text with the same font in the same size and the anti-aliasing is ok on all browsers. But not on my canvas except in firefox and IE. Chrome fail anti aliasing on canvas.

ghost avatar Nov 27 '12 16:11 ghost

Unfortunately, I'm using a Mac at work and in Chrome everything looks fine. However, assuming you're using a PC doesn't really matter, as the text should render the same both in the canvas and in the DOM.

Can you provide the @font-face CSS code as you're using it? Perhaps you missed something. Also, which font are you using? I noticed that Chrome doesn't like the format tag in @font-face (adding it actually broke my font). Too bad too, you almost have to use some sort of browser selection / distinction when using any CSS these days. They all render different and all seem to follow their own rules when it comes to the newer W3C stuff (CSS3 / HTML5). And in some cases, even the old stuff (pixel alignment is a pain when using position:absolute).

lazerblade01 avatar Nov 27 '12 21:11 lazerblade01

Here is my code (from fontssquirrel) :

@font-face { font-family: 'CalligraffitiRegular'; src: url('../../fonts/Calligraffiti.eot'); src: url('../../fonts/Calligraffiti.eot?#iefix') format('embedded-opentype'), url('../../fonts/Calligraffiti.woff') format('woff'), url('../../fonts/Calligraffiti.ttf') format('truetype'), url('../../fonts/Calligraffiti.svg#CalligraffitiRegular') format('svg'); font-weight: normal; font-style: normal; }

All fonts are wrong in my canvas in Chrome (arial, times ...) but in a div or a p, it's fine...

lazerblade01 thx for your help

ghost avatar Nov 28 '12 07:11 ghost

When I save image via toDataUrl function, the result is different on IE and Chrome, same thing on Chrome no anti aliasing...

ghost avatar Nov 30 '12 12:11 ghost

I use Google Web Fonts and the font-weight attribute (fontWeight in Kinetic parlance) would help a lot.

For example, needing the Dosis font in weights 400 (default in Kinetic) and 800 (extra chunky; nice for headers) as well as the Rufina font in the default of 400, the link/api call would then be:

gamecubate avatar Jan 22 '13 17:01 gamecubate

These can be set in the css for the page.

@font-face {
font-family: 'Exo';
src: url('/css/fonts/Exo-Medium-webfont.eot');
src: url('/css/fonts/Exo-Medium-webfont.eot?#iefix') format('embedded-opentype'),
     url('/css/fonts/Exo-Medium-webfont.woff') format('woff'),
     url('/css/fonts/Exo-Medium.otf') format('opentype'),
     url('/css/fonts/Exo-Medium-webfont.svg') format('svg');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'Exo';
src: url('/css/fonts/Exo-Bold-webfont..eot');
src: url('/css/fonts/Exo-Bold-webfont.eot?#iefix') format('embedded-opentype'),
     url('/css/fonts/Exo-Bold-webfont.woff') format('woff'),
     url('/css/fonts/Exo-Bold.otf') format('opentype'),
     url('/css/fonts/Exo-Bold-webfont.svg') format('svg');
font-weight: bold;
font-style: normal;
}

lazerblade01 avatar Jan 22 '13 19:01 lazerblade01

Had the same problem here. As Kinetic 4.5.1 i first had to draw my shape and it's not until I try to animate it that the custom font kicks in and is cached.

http://jsfiddle.net/hAWvr/2/

Event with the preloading of an element with this font before any script it won't be ready on first run when Domready is called.

http://jsfiddle.net/hAWvr/4/

The only option is to delay all drawings for the first time the font is loaded in this tab.

http://jsfiddle.net/hAWvr/5/

For a much accurate delay I used google webfont loader: http://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js

then specify

    WebFontConfig = {
      custom: { families: ['cabinregular'],
      urls: [ './styles.css' ] },
      active: function(){
        //do whatever you want here, init cloud & stuff.
      }
    };

Elyx0 avatar May 14 '13 05:05 Elyx0