Saturday, May 11, 2013

How to get coordinates of a mouse click on a canvas element

I have a problem with canvas and handling clicks. It looks that there is no easy way to get a relative coordinate of click inside a given DOM Element. After Googling a little, I've choosen the below solution:

The findRealOffset should be called everytime a position of canvas or layout of website changes.

var totalOffsetX = 0;
var totalOffsetY = 0;

findRealOffset() {
  var canvasX = 0;
  var canvasY = 0;
  var currentElement = canvas;
  while (currentElement != null) {
    totalOffsetX += currentElement.offsetLeft;
    totalOffsetY += currentElement.offsetTop;
    currentElement = currentElement.offsetParent;
  }
}

relativeCoord(e) {
  var canvasX = e.pageX - totalOffsetX;
  var canvasY = e.pageY - totalOffsetY;

  // Fix for variable canvas width
  canvasX = ( canvasX * (canvas.width / canvas.offsetWidth) ).round();
  canvasY = ( canvasY * (canvas.height / canvas.offsetHeight) ).round();
  return {'x':canvasX, 'y':canvasY};
}

Source: http://stackoverflow.com/questions/55677/how-do-i-get-the-coordinates-of-a-mouse-click-on-a-canvas-element

Wednesday, August 15, 2012

"datastore: invalid entity type"

The below error message is defined in datastore package of AppEngine.
ErrInvalidEntityType = errors.New("datastore: invalid entity type")
If by the accident one wrote:
data := Data{}
err := datastore.Get(c, key, data)

One will get above error message. The correct code looks as below:
data := Data{}
err := datastore.Get(c, key, &data)

The change is third passed parameter.

According to doc:
Get loads the entity stored for k into dst, which must be a struct pointer or implement PropertyLoadSaver.

Friday, August 10, 2012

Simple RegExp in Go

package main

import (
  "fmt"
  "regexp"
)

func main() {
  re := regexp.MustCompile("/page/(?P[\\d]+)/")
  b := []byte("/page/1/")
  x := re.Find(b)
  fmt.Printf("Found: `%s`\n", x)
  x1 := re.FindAll(b, 100)
  fmt.Printf("Found: `%s`\n", x1)
  fmt.Printf("Subexp num: `%d`\n", re.NumSubexp())
  x2 := re.FindAllSubmatch(b, 100)
  fmt.Printf("Found: `%s`\n", x2)
  fmt.Printf("Subexp names: %s", re.SubexpNames())
}

And output:
Found: `/page/1/`
Found: `[/page/1/]`
Subexp num: `1`
Found: `[[/page/1/ 1]]`
Subexp names: [ pagenr]
You can try it here: http://play.golang.org/p/SqUe4X7vuR

Monday, June 18, 2012

Turn off "Pissing off flash content".

Have you ever experienced that annoying sound when you enter a web page? Probably not just once. But, there's a solution waiting for you to apply!

If you use Google Chrome, go to chrome://chrome/settings/content and in a section "Plug-ins" check option "Click to play". The flash content will be run only if you click on it. Additionally click "Manage exceptions..." and add domains for which you want to allow/deny flash (e.g. www.youtube.com).

Now you can browse in peace ;-)

Friday, April 20, 2012

Remove "edit" link from wiki site.

Paste below code to JavaScript console.
f = function(l) {
  for(var i = 0; i < l.length; i++) {
    var c = l[i];
    c.parentNode.removeChild(c);
  }
};

f($('.sectionedit'));