The story of Onan upholds the fact that performing coitus interruptus and ejaculating onto the ground when your father commands you to impregnate your levirate wife is a sin. It has no application to masturbation, unless one is masturbating to avoid impregnating his levirate wife and thus deny his dead brother a legal son.
(Fun fact: After going through two husbands, the woman in the story disguised herself as a prostitute and had sex with their father. This produced the sought-after sons, one of whom was an ancestor of Jesus·)
Catholic teaching is wasting seed, BY ANY MEANS, is a sin.
>>However, other early writers have sometimes focused on the spilling seed, and the sexual act being used for non-procreational purposes. One opinion expressed in the Talmud argues that this was where the death penalty’s imposition originated.[9] This interpretation was held by several early Christian apologists. Jerome, for example, argued:
But I wonder why he the heretic Jovinianus set Judah and Tamar before us for an example, unless perchance even harlots give him pleasure; or Onan, who was slain because he grudged his brother his seed. Does he imagine that we approve of any sexual intercourse except for the procreation of children?[10]
Clement of Alexandria, while not making explicit reference to Onan, similarly reflects an early Christian view of the abhorrence of ‘”spilling seed’”:
Because of its divine institution for the propagation of man, the seed is not to be vainly ejaculated, nor is it to be damaged, nor is it to be wasted[11]
To have coitus other than to procreate children is to do injury to nature’[12]
Many Christian groups have cited the Onan narrative as justification for bans on both masturbation and coitus interruptus, and, since medieval times, have also used it to justify a prohibition against contraception. This view that wasted seed refers to masturbation was upheld by many early rabbis. However, the Levitical regulations concerning ejaculation, whether as a result of heterosexual intercourse[13] or not,[14] merely prescribe a ritual washing, and remaining ritually impure until the next day began on the following evening.<< Wikipedia