chaining methods and properties in coffescript - coffeescript

What would be the best method to DRY out or chain this code using Coffeescript?
canvas=$('canvas')
canvas.getContext('2d').fillStyle='rgba(255,255,255,0.7)'
canvas.getContext('2d').font = "40px Verdana"
canvas.getContext('2d').fillText('alkatsa.com',canvas.width/2 - 120,canvas.height - 50)

I believe there’s no chaining of properties, only of methods. I’d make variables for context, width and height. This would make code more vertical:
canvas = $ 'canvas'
{width, height} = canvas
context = canvas.getContext '2d'
context.fillStyle = 'rgba(255, 255, 255, 0.7)'
context.font = '40px Verdana'
context.fillText 'example.com', width / 2 - 120, height - 50

Related

pyglet label not showing on screen on draw() with OpenAI gym render

I'm using a wrapper of OpenAI gym to create a graphical representation of a problem. For that, I'm drawing a circle with the given wrapper and then try to add some text.
My issue is, that only the circle shows, but the text label does not appear. Any suggestions on how I can make the text visible would be highly appreciated. Below a minimal example, that show the problem:
import pyglet
from gym.envs.classic_control import rendering
screen_width = 600
screen_height = 400
table_radius = 200
viewer = rendering.Viewer(screen_width, screen_height + 20)
table = rendering.make_circle(radius=table_radius, filled=False)
table_trans = rendering.Transform()
table.add_attr(table_trans)
table_trans.set_translation(screen_width / 2, screen_height / 2)
viewer.add_geom(table)
text = 'This is a test but it is not visible'
label = pyglet.text.Label(text, font_size=36,
x=10, y=10, anchor_x='left', anchor_y='bottom',
color=(255, 123, 255, 255))
label.draw()
viewer.render(return_rgb_array=False)
input()
What worked for me is to manually implement the render function and insert the label after the tranform.disable()
## Content of the render function
glClearColor(1, 1, 1, 1)
self.viewer.window.clear()
self.viewer.window.switch_to()
self.viewer.window.dispatch_events()
self.viewer.transform.enable()
for geom in self.viewer.geoms:
geom.render()
for geom in self.viewer.onetime_geoms:
geom.render()
self.viewer.transform.disable()
# Text
label = pyglet.text.Label(
"Hello, world",
font_name="Times New Roman",
font_size=36,
x=100,
y=100,
anchor_x="center",
anchor_y="center",
color=(255, 0, 0, 255),
)
label.draw()
arr = None
...
please try this code:
import pyglet
from gym.envs.classic_control import rendering
class DrawText:
def __init__(self, label:pyglet.text.Label):
self.label=label
def render(self):
self.label.draw()
screen_width = 600
screen_height = 400
table_radius = 200
viewer = rendering.Viewer(screen_width, screen_height + 20)
table = rendering.make_circle(radius=table_radius, filled=False)
table_trans = rendering.Transform()
table.add_attr(table_trans)
table_trans.set_translation(screen_width / 2, screen_height / 2)
viewer.add_geom(table)
text = 'This is a test but it is not visible'
label = pyglet.text.Label(text, font_size=36,
x=10, y=10, anchor_x='left', anchor_y='bottom',
color=(255, 123, 255, 255))
label.draw()
viewer.add_geom(DrawText(label))
viewer.render(return_rgb_array=False)
input()

ExtJS - Attributes changes are being ignored during drag action

We are using ExtJS 4.2.1. The width/height attributes of the img won't change in below example when element is dragged beyond the +/-5px y-coordinate. Changes to attribute 'show' are also ignored. However, element can be destroyed and re-created, but this is not desired.
[panel]
var dndLinkSprite = me.surface.add({
type: 'image',
x: bBox.x,
y: bBox.y,
width: 16,
height: 16,
src: '/link.png'
})
...
dragAction: function(panel, e, diff, dndConfig) {
var spriteLink = panel.dndLinkSprite;
if ( diff[1] > 5 || diff[1] < -5 ) {
spriteLink.setAttributes(height, 16);
spriteLink.setAttributes(width, 16);
} else {
spriteLink.setAttributes(height, 0);
spriteLink.setAttributes(width, 0);
};
}
Thanks for your help!
Solved - wrong syntax:
Instead of:
spriteLink.setAttributes(height, 0);
spriteLink.setAttributes(width, 0);
use:
spriteLink.setAttributes({width: 0, height:0} , true);
spriteLink.getEl().dom.setAttribute(height, 16);

NSTextField non-system-font content clipped when usesSingleLineMode is true

Setting usesSingleLineMode to true for a non-system font causes the top of the text to be clipped.
I've created 3 very simple test cases that illustrate this:
good : non-system font, with usesSingleLineMode = false. Works fine.
bad : non-system font with usesSingleLineMode = true. Does not work.
system : system font with usesSingleLineMode = true. Works fine.
Add the following to the viewDidLoad() method of a new Cocoa OSX application:
// Do any additional setup after loading the view.
let good = NSTextField(frame: NSRect(x: 0, y: 0, width: 800, height: 55))
good.usesSingleLineMode = false
good.font = NSFont(name: "HelveticaNeue-UltraLight", size: 24)
good.stringValue = "Good usesSingleLineMode false "
self.view.addSubview(good)
let bad = NSTextField(frame: NSRect(x: 0, y: 100, width: 800, height: 55))
bad.usesSingleLineMode = true
bad.font = NSFont(name: "HelveticaNeue-UltraLight", size: 24)
bad.stringValue = "Bad usesSingleLineMode true"
self.view.addSubview(bad)
let system = NSTextField(frame: NSRect(x: 0, y: 200, width: 800, height: 55))
system.usesSingleLineMode = true
system.font = NSFont.systemFontOfSize(24)
system.stringValue = "Good usesSingleLineMode true, System Font"
self.view.addSubview(system)
If I create the same bad NSTextField using Interface Builder in a storyboard, set the font in IB and check Uses Single Line Mode in IB it works fine! But, it would be impractical to build the overall view in IB, thus I want to programmatically create it.
Why is this happening? Have I missed some important setting (I've tried adjusting many NSTextField and NSTextFieldCell parameters to no avail? Is there a workaround?
According to Apple themselves, this is correct and even desired behavior:
Engineering has determined that this issue behaves as intended based
on the following information:
This behaves correctly according to the documentation for NSCell:
Cells in the single line mode use the fixed baseline layout. The text
baseline position is determined solely by the control size regardless
of content font style or size.
Source: http://www.openradar.me/13813516
What the documentation says is really correct but the important detail here is what the documentation does not say. It does say that the "text baseline position is determined solely by the control size" but it does not explain in detail how this is done. And it is a known fact, that the baseline always seems to fit correctly to the system font, yet it hardly ever fits to any other font on your system. The problem is that Apple speaks of "the fixed baseline layout", as if that would be something known and well documented, but it isn't. I haven't found any document, not even among the legacy ones, that would explain the fixed baseline layout.
I solved it the problem by setting usesSingleLineMode to false.

AsyncDisplayKit - ASButton Size

Table Cell
I'm learning to use Asyncdisplaykit. My result is in picture below.
But I don't know how to constraint button equal Screen.width/3, Its auto constraints equal image width.
This is my code
let controlStack = ASStackLayoutSpec(direction: ASStackLayoutDirection.Horizontal, spacing: 3.0, justifyContent: ASStackLayoutJustifyContent.Center, alignItems: ASStackLayoutAlignItems.Center, children: [self.mFavoriteButton, self.mCommentButton, self.mShareButton])
controlStack.spacingAfter = 3.0
controlStack.spacingBefore = 3.0
let insetDateLayout = ASInsetLayoutSpec(insets: UIEdgeInsets(top: 10, left: self.frame.size.width - 100, bottom: 0, right: 0), child: self.mDateTimeNode)
let imageLayout = ASStaticLayoutSpec(children: [imagePlace, insetDateLayout])
return ASStackLayoutSpec(direction: .Vertical, spacing: 3.0, justifyContent: ASStackLayoutJustifyContent.Start, alignItems: ASStackLayoutAlignItems.Center, children:[imageLayout, self.mMessageNode, controlStack])
try change ASStackLayoutAlignItems.Center to ASStackLayoutAlignItems.Stretch. And try enable Grow Constraints for buttons (need to test it):
self.mFavoriteButton.flexGrow = true;
self.mCommentButton.flexGrow = true;
self.mShareButton.flexGrow = true;
Reply if you were able to address the problem!
Typically you want to use flexBasis with a relative size of 0.33 to do 1/3rd. Check out documentation on Web / CSS flexbox for examples of flexbasis.

How to set line height in Clutter.Text using Pango

I want to have a Clutter.Text displaying text with double line spacing using Gtk.Pango.
note : in css, we would use line-height: 200% I think.
I tried this code and it didn't work:
var text_actor = new Clutter.Text.with_text ("Roboto 10", "Long long text") ;
text_actor.width= SIDE_PANE_WIDTH ;
text_actor.get_layout ().set_spacing (2*Pango.SCALE) ;
Apparently, Clutter.Text keeps a cached version of the Pango.Layout
Is there a way to achieve this?
One way working around this by using CoglPango directly:
var t = new Clutter.Actor ();
t.set_size (300, 300);
// add some more text to see it
var text = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, ...";
var layout = t.create_pango_layout (text);
layout.set_width (200);
layout.set_spacing (24 * Pango.SCALE);
t.paint.connect (() => {
Cogl.pango_render_layout (layout, 0, 0,
Cogl.Color.from_4ub(0, 0, 0, 255), 0);
});
edit this is indeed the solution. Based on this, here's the code that I used:
actor = new Text () ;
actor.width= SIDE_PANE_WIDTH ;
var text = "Some long text<b>Bold</b>" ;
var pango_layout = bio_actor.create_pango_layout ("");
pango_layout.set_markup (text, text.length) ;
pango_layout.set_spacing (2 * Pango.SCALE);
pango_layout.set_font_description (Pango.FontDescription.from_string ("Roboto 10")) ;
pango_layout.set_width (SIDE_PANE_WIDTH * Pango.SCALE);
bio_actor.paint.connect (() => {
Cogl.pango_render_layout (pango_layout, 0, 0, Cogl.Color.from_4ub(255, 255, 255, 255), 0);
});
int width;
int height ;
pango_layout.get_size (out width, out height) ;
actor.height = height / Pango.SCALE;
Yes, ClutterText caches the PangoLayout instance to avoid having to re-measure text every time it has to retrieve the preferred size. It actually caches the latest three instances of PangoLayout, because of the width-for-height/height-for-width geometry requests, but that's just an implementation detail.
Changing properties on the PangoLayout instance of ClutterText is also not going to result in updates to the ClutterText: you'd have to call clutter_actor_queue_redraw() afterwards, as the PangoLayout sits at a lower level than the actor in the scene graph.
In general, the ClutterText actor does not allow you to tweak the PangoLayout it creates; it's expected that the actor is in charge of creating an modifying the layout, and attempts at overriding that will result in either poor performance or undefined behaviour.
If you want to introduce a line spacing property then you'll have to file a bug against ClutterText — though that would mean that you'd have to wait until September for the next stable release of Clutter.
If you don't need all that ClutterText does, and you just want to display text, you could subclass ClutterActor and use clutter_actor_create_pango_layout() to create a PangoLayout that you can measure inside the overridden get_preferred_width(), get_preferred_height(), and allocate() virtual functions, and paint inside the overridden paint() virtual function.